[Haskell-cafe] Modelling Java Interfaces with Existential data types

Tom Pledger tpledger at ihug.co.nz
Tue Jun 8 22:33:35 EDT 2004


Another way is to take advantage of Haskell's laziness:

> class Foo a where
>     foo :: a -> Int
> data Oof1 = Oof1 Int
> instance Foo Oof1 where foo (Oof1 i) = i
> data Oof2 = Oof2 Int
> instance Foo Oof2 where foo (Oof2 i) = i
> list1 = [foo (Oof1 10), foo (Oof2 20)]

That's all it takes. The applications of foo in list1 don't get 
evaluated until list1 is used, e.g. by sum.


More information about the Haskell-Cafe mailing list