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

TJ tjay.dreaming at gmail.com
Fri Oct 19 10:51:23 EDT 2007


Why is it illegal to store values of differing types, but which
instance the same class, into a list? e.g.

a = [ 1, 2.0 ] :: Num a => [a]

After all, sometimes all you need to know about a list is that all the
elements support a common set of operations. If I'm implementing a 3d
renderer for example, I'd like to have

class Renderable a where
  render :: a -> RasterImage

scene :: Renderable a => [a]


Instead of hardcoding a bunch of types as being Renderable, as in

data Renderable
  = Point Something
  | Line Something
  | Polygon Something

scene :: [Renderable]


Or maybe

data Point = Point Something
data Line = Line Something
data Polygon = Polygon Something

scene :: { points :: [Point], lines :: [Line], polygons :: [Polygons] }


Is there a way of achieving what I want to do? Existentials maybe? I'm
still learning the basic stuff and don't grok existentials at all, but
I even if I use those, I'll still have to wrap things up in a
constructor, won't I?


Thanks a bunch,

TJ


More information about the Haskell-Cafe mailing list