Classes and interfaces, a naming restriction

Simon Peyton-Jones
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.


| Here's what I'd like to do:
| class IStream stream char where
|   readStream :: stream -> Int -> IO [char]
| data IStream char =3D forall stream. IStream stream char =3D> IStream
| instance IStream (IStream char) char where
|   readStream (IStream stream) =3D readStream stream
| 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
| 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)
| 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
| Do I really have to use data IStreamWrapper =3D ... instead? :-(
| Duncan
| _______________________________________________
| Glasgow-haskell-users mailing list