[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Curl, multiple inheritance, and interfaces (was Re: cheerful static typing)
- To: address@hidden
- Subject: Re: Curl, multiple inheritance, and interfaces (was Re: cheerful static typing)
- From: "Krishnaswami, Neel" <address@hidden>
- Date: Fri, 11 Jan 2002 15:35:47 -0500
- Sender: address@hidden
Christopher Barber [mailto:email@example.com] wrote:
> > If you're suggesting that simply because a class happens to
> > include methods that match a particular interface, that it
> > should satisfy that interface, I don't think that's a very good
> > idea.
> That is what I am suggesting. I have mixed feelings about
> it, but many would argue this is a useful concept.
One of the things I really like about Haskell is that you can
declare interfaces *after* a type is declared.
class Frobbable a where
frob :: a -> String
Then you can add types to the Frobbable interface with instance
instance Frobbable Integer where
frob n = int_to_string n
instance Frobbable String where
frob s = concatenate s s
Then the function frob has type 'Frobbable a => a -> String', which
means "For every type a that is an instance of the type class
Frobbable, frob has type a -> String."
Thus frob 31246 --> "31246"
and frob "foo" --> "foofoo"
So you can define an interface with a type class, and then clients
can retrofit existing types into the interface by adding instance
declarations for their preexisting type.
This style of programming is familiar to users of generic-function
OO, who are used to specifying protocols as sets of generic functions.
Type classes let you specify the protocol in the actual code, and in
fact, I've often wished for precisely this capability when hacking