What I would like to do is have a function that takes a list of
"objects", where the objects are all instances of a particular class,
but not necessarily the same type. Example:

class Shape a where
  extent :: a -> Point
  ...
biggest :: Shape a => [a] -> Point
biggest listOfShapes = ...

The closest I could come to achieving this in standard Haskell is:

data Shape = Shape {extent :: Point, ...}
class ShapeClass a where
  mkShape :: a -> Shape
biggest :: [Shape] -> Point
biggest listOfShapes = ...

The problem with this is that the caller of `biggest' needs to call
`mkShape' on each element in the list before it can construct the list.
Not _bad_, but rather unwieldy. The caller can't simply use `map' either
if each element is of a different type.

Anyone know of an elegant way to do this? I'd prefer to stay away from
compiler-specific features, but if something like existential types is
just what I need, I guess I can bite the bullet.

Thanks,
- Michael Hobbs


Reply via email to