[Haskell-cafe] Polymorphic (typeclass) values in a list?

Tim Docker timd at macquarie.com.au
Mon Oct 22 01:26:13 EDT 2007


TJ:

> Ah... indeed it can, in this case. It won't work if class Renderable
> also has a method for saving to file, etc, I suppose, unless scene ::
> [(RasterImage,IO (),...whatever other operations...)]

In this case I would generally create a record:

data Renderable = Renderable {
    image :: RasterImage,
    saveToFile :: FilePath -> IO (),
    ... etc ...
}

scene :: [Renderable]

You may then like to add a type class to turn things into renderables:

class IsRenderable where
    toRenderable :: a -> Renderable

instance IsRendeable Point where ...
instance IsRenderable Line where ...

It depends on your needs, but in my limited experience, records are
often more convenient for emulating OO-style programming than are type
classes.

Tim


More information about the Haskell-Cafe mailing list