[Haskell-cafe] How to present the commonness of some objects?

Luke Palmer lrpalmer at gmail.com
Fri Jul 3 15:29:35 EDT 2009

2009/7/3 Ross Mellgren <rmm-haskell at z.odi.ac>

> Wordy (and yet technically accurate) names aside, isn't this basically the
> same thing, except that you must pass the dictionary around by hand?

A SomeWidget is defined as "any object which has a Widget dictionary".  It's
still an object; the link from it to its dictionary is implicit.  But since
you have no other qualifiers on that object, nothing can be determined from
it but its dictionary. Why not just junk the indirection and make the object
equal to its dictionary.

This is a different story if you a class like:

class Split a where
    split :: a -> (a,a)
    join :: a -> a -> a
data SomeSplit = forall a. Split a => SomeSplit a

Here a SomeSplit can be split into two SomeSplits, but two SomeSplits can't
be joined into one.  Two join two of these things, you must have split them
off a common ancestor.

> What is the advantage of doing the dictionary passing manually, other than
> being able to avoid the scoping issue (that requires case) and the slightly
> odd syntax?

The fact that it's exactly the same, except for the scoping issue and the
slightly odd syntax.  You're not saving any parameter passing.

> To expand your example, would you suggest something like:
> data Widget = Widget { widgetRun :: IO () }
> data Label = Label (String -> IO ())
> data Button = Button (IO ())

> labelToWidget = Widget runLabel
> buttonToWidget = Widget runButton
> widgetList :: [(Integer, Integer, Widget)]
> widgetList = [labelToWidget myLabel, buttonToWidget myButton]

Yeah sure, something like that.

Except, concretely, I don't see how a Label is a String -> IO ().  Is that a
setter function for its text?  How is a Widget going to use that. I guess
unless a "label widget" passed *you* a label when you create it.  I'd say
the other option in this paradigm is a MVar.  But I digress...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090703/64c8176f/attachment.html

More information about the Haskell-Cafe mailing list