On Wed, Jul 1, 2015 at 6:03 AM, Giuseppe Castagna <
g...@pps.univ-paris-diderot.fr> wrote:

> On 30/06/15 22:30, yary wrote:
>
> Now that I've read ahead to 3.4, the "multi method solution" shown can be
> a little simpler, just need to add "multi" to the original "equal" methods,
> see attached.
>
>  Indeed, nicely seen. However this solution is not modular since it
> requires the modification of an "existing" class. See the paragraph I wrote
> before the excursus in Section 3.3
>

Point taken. I'd still like to employ "don't repeat yourself" and have
ColPoint's multi-method "equal" call Point's method "equal," but I haven't
been able to get this code to work. Perl people, can you debug this example
so that the call to "$a.equal($b)" returns True in some elegant way,
meaning, not explicitly naming "equal" or "Point" in the body of ColPoint's
"multi method equal(Point $p)", nor repeating Point.equal's code either,
nor touching the the Point class in any way?

-y


#!perl6
class Point {
  has $.x is rw;
  has $.y is rw;

  method equal(Point $p) {
    say "In Point.equal";
    ($.x==$p.x)&&($.y==$p.y)
  }
};

class ColPoint is Point {
  has Str $.c is rw;

  multi method equal(ColPoint $p) {
    say "In ColPoint.equal";
    ($.x==$p.x)&&($.y==$p.y)&&($.c eq $p.c)
  }

  multi method equal(Point $p) {
    nextsame; # want this to call Point.equal
  }
};


my Point $a = ColPoint.new(x=>2,y=>21,c=>"white");
my Point $b = Point.new(x=>2,y=>21);
say $a.equal($b); # Should say True

Reply via email to