[Haskell-cafe] Re: [Haskell] Dynamic binding
rossberg at ps.uni-sb.de
Fri Jun 24 06:00:33 EDT 2005
Ralf Lammel wrote:
> Can you just tell how *you* would favor encoding the shapes example
> that was posed by poster? (It might just be that your code would be
> very close to Lennart's proposal?)
There is no universal answer. The shape example obviously is just a toy
example. As long as I have no idea what *concrete* problem the original
poster is trying to solve, I cannot really tell which approach is
preferable for his endeavour. It seems more appropriate to describe the
options and their trade-offs.
First of all, note that very often, you do not need a heterogeneous
collection at all. Then plain polymorphism with type classes is more
than enough, and more than OO provides in that situation.
In many cases where you need some kind of heterogenicity (is that a
word?), the standard datatype approach shown by Lennart is perfectly
suitable - in fact, often much more suitable than the OO solution. You
know about the "expression problem", and the two dual notions of
extensibility? OO supports one dimension, datatypes the other. It
depends on the problem which one is more important.
When you really need OO-style intensional kind of behaviour selection
then first-class functions can bring you quite a long way, and often
with much less amount of boilerplate than typical OO solutions.
When the behaviour you have to encapsulate in heterogeneous collection
becomes more complex - say, more then just one or two functions -
first-class functions can be tedious. Existential types represent a more
coarse-grained and structured variant of the first-class function
approach. They combine the power of first-class functions with the
convenience of type classes, very similar to class-based objects. In
these cases, they are the most appropriate solution.
Note again that with the latter two solutions, as with the OO approach,
you do not have extensibility on the operations dimension.
Andreas Rossberg, rossberg at ps.uni-sb.de
Let's get rid of those possible thingies! -- TB
More information about the Haskell-Cafe