[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Curl, multiple inheritance, and interfaces (was Re: cheerful static typing)

Christopher Barber [mailto:cbarber@curl.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 
in Dylan.

Neel Krishnaswami