in reply to Re: Perl6 - value vs. reference issues
in thread Perl6 - value vs. reference issues

Today in "use overload", the so-called assignment operator can be defined. If an overloaded operator is called on an object that is not unique but shares a reference, this function is called to duplicate it. That is, it's uses as a lazy copy-on-write mechanism.

This means that Perl must know that the reference is shared, and which functions are indeed "mutators". I can have a complex number class appear to have value semantics when an assignment is followed by an overloaded operator such as "+", but not for a named method call.

So, something like

$a= $b; ++ $a;
can indeed be made to work like built-in numbers, calling
$a= $b; $a->set_imaginary (5);
cannot, and would set the imaginary part of $b as well.

Perhaps Perl6 simply needs to have the lazy copy function applied uniformly whenever a mutator is called, whether you must identify mutators or it figures it out by itself.

Meanwhile, why write the copy code every time? Simply saying that a class is "by value" should be enough to automatically generate the copy function, which copies all the data members of the object.

—John