[Haskell-cafe] OOP'er with (hopefully) trivial questions.....

Ketil Malde ketil+haskell at ii.uib.no
Mon Dec 17 18:36:29 EST 2007


Miguel Mitrofanov <miguelimo38 at yandex.ru> writes:

> I've noticed it, but there are some problems with this
> representation, so I decided not to mention it. It's OK as far as we
> don't want functions working on two areas - I don't see, how we can
> implement, say, intersect :: Shape -> Shape -> Bool in this way.
> However, it's a useful pattern.

Yes, there are different trade offs, it depends what you want to do.
The AlgDT makes intersect simple:

    intersect :: Shape -> Shape -> Bool
    intersect (Circle x) (Circle y) = ...
    intersect (Circle x) (Rectangle x y) = ...
    :

As Derek hints at, this isn't so nice in C++ and friends, you probably
will end up with

  // Apologies for any mistakes in the code, it's been a while.
  class Circle : public Shape {
    :
    bool intersect(s){
       if(dynamic_cast<Circle>(s)){...}
       else if (dynamic_cast<Rectangle>(s)){...}
       :
       }
  }

etc.  In addition to being very verbose and tedious code to write, you
will have no idea if you have managed to cover all cases.  Your
'intersect' function is spread all over the place.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list