[Haskell-cafe] typeclass question
twd2 at dockerz.net
Thu Sep 11 06:18:30 EDT 2008
I have a typeclass related question that I have been puzzling over.
In a library I am working on, I have a series of functions for
converting values to Renderables:
| labelToRenderable :: Label -> Renderable
| legendToRenderable :: Legend -> Renderable
| axisToRenderable :: Axis v -> Renderable
| layoutToRenderable :: Layout x y -> Renderable
These names are overloaded for convenience via a typeclass:
| class ToRenderable a where
| toRenderable :: a -> Renderable
| instance ToRenderable Label where
| toRenderable = labelToRenderable
But some recent changes mean that Renderable needs to become a type
constructor, and the functions now product different types:
| labelToRenderable :: Label -> Renderable ()
| legendToRenderable :: Legend -> Renderable String
| axisToRenderable :: Axis v -> Renderable ()
| layoutToRenderable :: Layout x y a -> Renderable a
Is there a nice way to overload a "toRenderable" function for these?
Something like this is possible:
| class ToRenderable a b where
| toRenderable :: a -> Renderable b
But the above is, I think, too general for my needs. I don't want
to be able to generate Renderables of different type b for a single input
Also, MPTC take me out of the world of haskell 98, which I was trying
to avoid. Am I missing something simple?
Any pointers would be much appreciated.
More information about the Haskell-Cafe