On 7/11/05, Ingo Blechschmidt <[EMAIL PROTECTED]> wrote:
>   class Foo {
>     has $.data;
>     method incr () { $.data++ }
> 
>     # Please fill in appropriate magic here
>   }
> 
>   my Foo $x .= new(:data(42));
>   my Foo $y  = $x;
>   $y.incr();
>   say $x.data;    # Should still be 42
>   say $x =:= $y;  # Should be false

Whoops, I didn't read that carefully enough.  You shouldn't mutate
inside a value type (the body of incr() is a no-no).  Ideally, it
should be implemented like so:

    class Foo is value {
        has $.data;
        method succ () { $.data + 1 }
    }
    my $x = Foo.new(data => 42);
    my $y = $x;
    $y.=succ;
    ...

Then you don't need any special magic.  But if you want to have
mutator methods that aren't in .= form (which I recommend against for
value types), maybe the `is value` trait makes the class do the
`value` role, which gives you a method, `value_clone` or something,
with which you specify that you're about to mutate this object, and
that Perl should do any copying it needs to now.

Luke

Reply via email to