using ghc with make

Brian Hulley brianh at metamilk.com
Thu Apr 20 14:07:28 EDT 2006


Bulat Ziganshin wrote:
> i'm tried to say that there is no such dynamic beast as virtual
> functions in C++

I think you can use existential types to simulate virtual functions:

-- 'a' is a stream of b's
class StreamClass a b where
     get :: a -> IO b

-- hide the particular 'a' to get any stream of b's
data Stream b = forall a. StreamClass a b => Stream a

-- a concrete 'a'
data TextStream = ...
instance StreamClass TextStream Char where
     get ...

-- another concrete 'a'
instance StreamClass MemoryStream Char where
    get ...

-- concrete 'a' is hidden just as in C++ virtual dispatch
useStream :: Stream b -> IO b
useStream (Stream x) = get x -- because x satisfies StreamClass x b

Regards, Brian.


More information about the Glasgow-haskell-users mailing list