[Haskell] Dynamic binding
Andrew Ward
andy.ward at ihug.co.nz
Wed Jun 22 21:38:03 EDT 2005
Pal-Kristian Engstad wrote:
>On Wednesday 22 June 2005 05:38 pm, Andrew Ward wrote:
>
>
>>What would be the normal way for a Haskell programmer to handle the
>>typical shape example in beginner OO tutorials?
>>
>>
>
>By not doing OO. You have to ask yourself, what is the purpose and/or benefit
>of using OO? In C++, OO is _useful_ because you restrict the problems of
>mutable data (by enclosing it in C++ classes).
>
>ML type languages have other methods of doing things, and guess what, OO is
>not that needed for these languages. Sum-types, pattern-matching and data
>constructors make half of the need for OO go away. Higher order functions and
>make it even less needed. For the rest, there's always work-arounds.
>
>PKE.
>
>
To handle the problem of drawing all shapes, in c++, I would have a list
of shape pointers:
struct shape{ virtual void draw(...);};
struct circle : public shape {...};
struct square : public shape {...};
std::list<shape *> shapes;
for(std::list<shape *>::iterator it = shapes.begin();it !=
shapes.end();++it)
{ (*it)->draw(...); }
This general pattern of dynamic binding I use over and over again. Could
you give me some example code of this type of thing handled in Haskell's
way? Assuming that the number of classes deriving from shape might get
quite large.
Andrew Ward.
More information about the Haskell
mailing list