[Haskell-cafe] Lists of Existential DT

Jason Dagit dagit at codersbase.com
Sun Feb 28 16:18:50 EST 2010


On Sun, Feb 28, 2010 at 6:31 AM, Yves Parès <limestrael at gmail.com> wrote:

>
> Hello!
>
> I have a class Drawable, and some datatypes which are instances of it, and
> I
> would like to be able to draw them all at once!
> drawMany window [image, text, otherImage]
>
> I think the type of the function drawMany would be:
> drawMany :: Window -> [forall a. (Drawable a) => a] -> IO ()
>
> However it doesn't work.
> I know one solution is to make a new datatype (e.g. DrawableObj) which will
> be:
> data DrawableObj = forall a (Drawable a) =>
>                              DrawableObj a
>
> And then declare drawMany as:
> drawMany :: Window -> [DrawableObj] -> IO ()
>
> But to use it I have to wrap up myself every drawable in a DrawableObj:
> drawMany window [DrawableObj image, DrawableObj text, DrawableObj
> otherImage]
>
> Is there another more suitable way to handle a list of drawables?
>

Luke Palmer has an article on his blog that proposes a different solution to
the problem that I believe you're trying to solve:
http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/

You might give his approach a try and see if it's simpler and easier to work
with.

Good luck!
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100228/99f0e9c1/attachment.html


More information about the Haskell-Cafe mailing list