Classes and interfaces, a naming restriction

Simon Peyton-Jones simonpj@microsoft.com
Tue, 22 Apr 2003 09:09:36 +0100


| Aparantly there's a restriction on having a class and a data type with
| the same name. A strange thing to want to do?

Maybe not, but illegal in Haskell.  I think the main reason is
distinguishing classes from instances in export and import lists:

	module Foo( IStream(..) ) where ...

What gets exported, the class IStream or the data type IStream?  =20

The best you can do is put them in separate modules.  Then you can name
them as M1.IStream and M2.IStream if you want.

Simon

| Here's what I'd like to do:
|=20
| class IStream stream char where
|   readStream :: stream -> Int -> IO [char]
|=20
| data IStream char =3D forall stream. IStream stream char =3D> IStream
stream
|=20
| instance IStream (IStream char) char where
|   readStream (IStream stream) =3D readStream stream
|=20
| So here the *type* IStream is the representative member of the *class*
| IStream. An OOP analogy is that a particular 'stream' such that
| (IStream stream char) =3D> stream
| is a particular concrete class that inherits from a virtual base class
IStream,
| but a 'stream' that is of type IStream is a pointer/reference to
something that
| inherits from the virtual base class (or a COM interface pointer)
|=20
| With the former we have (potentially) static binding, with the latter
it is necessarily
| dynamic binding. So whenever I need a hetrogenious collection of
streams I can apply:
| IStream :: forall c s. (IStream s c) =3D> s -> IStream c
|=20
| Do I really have to use data IStreamWrapper =3D ... instead? :-(
|=20
| Duncan
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users@haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users