Classes and interfaces, a naming restriction

Duncan Coutts duncan@coutts.uklinux.net
Tue, 22 Apr 2003 00:33:07 +0100


Hi,

Aparantly there's a restriction on having a class and a data type with
the same name. A strange thing to want to do?
Here's what I'd like to do:

class IStream stream char where
  readStream :: stream -> Int -> IO [char]

data IStream char = forall stream. IStream stream char => IStream stream

instance IStream (IStream char) char where
  readStream (IStream stream) = 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) => 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)

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) => s -> IStream c

Do I really have to use data IStreamWrapper = ... instead? :-(

Duncan