How do I... create a value type?

0 views
Skip to first unread message

Ingo Blechschmidt

unread,
Jul 11, 2005, 9:17:01 AM7/11/05
to perl6-l...@perl.org
Hi,

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

Luke Palmer

unread,
Jul 11, 2005, 1:43:24 PM7/11/05
to Ingo Blechschmidt, perl6-l...@perl.org
On 7/11/05, Ingo Blechschmidt <ibl...@web.de> wrote:
> Hi,
>
> 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
> }

I think it's just `class Foo is value`. It's probably wrong to do it
by overloading &infix:<=> like Perl 5 did.

Luke

Luke Palmer

unread,
Jul 11, 2005, 1:48:09 PM7/11/05
to Ingo Blechschmidt, perl6-l...@perl.org
On 7/11/05, Ingo Blechschmidt <ibl...@web.de> 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

Larry Wall

unread,
Jul 11, 2005, 3:30:03 PM7/11/05
to perl6-l...@perl.org
On Mon, Jul 11, 2005 at 11:48:09AM -0600, Luke Palmer wrote:
: 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.

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

Nicholas Clark

unread,
Jul 19, 2005, 2:55:37 PM7/19/05
to perl6-l...@perl.org
On Mon, Jul 11, 2005 at 12:30:03PM -0700, Larry Wall wrote:

> 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

Reply all
Reply to author
Forward
0 new messages