my $x = 42;
my $y = $x;
$y++;
say $x; # Still 42, of course
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
--Ingo
--
Linux, the choice of a GNU | The computer revolution is over.
generation on a dual AMD | The computers won.
Athlon! | -- Eduard Bloch
I think it's just `class Foo is value`. It's probably wrong to do it
by overloading &infix:<=> like Perl 5 did.
Luke
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
I think that's probably just "is cow", which is an autocloning variant
of "is rw"/"is constant". (Though somebody's metaclass somewhere would
have to take that class trait and apply it to any individual methods
that return and/or mutate the object in question.) But basically,
it's the same problem as wrapping a "constant" proxy, only the "cow"
proxy clones when the "constant" proxy would pitch a fit.
We specced an "is copy" on parameters at one point, but maybe that's
just "is cow" too. Or maybe they should all be "is copy", and the
COWness of it is just a lazy-style optimization that we should hide
from the user. People who haven't read the manual will probably grok
"is copy" better than "is cow" anyway, cute as it may be. So let's
maybe go with "is copy".
Or we go the other way and, in a binge of orthogonality, rationalize
all the "on write" traits:
Current Conjectural
======= ===========
is constant is dow "die on write"
is copy is cow "copy on write"
is rw is mow "mutate on write"
But thankfully Perl 6 is not intended to be a "perfect language".
Larry
> Or we go the other way and, in a binge of orthogonality, rationalize
> all the "on write" traits:
>
> Current Conjectural
> ======= ===========
> is constant is dow "die on write"
> is copy is cow "copy on write"
> is rw is mow "mutate on write"
>
> But thankfully Perl 6 is not intended to be a "perfect language".
That conjecture reminded me of Allison's "togs" and "dogs". I think I like
the verbose version we're going to keep, even though you are suggesting it
is *im*perfect. :-)
Nicholas Clark