in reply to Perl6 - value vs. reference issues

Some handwaving in overloading mitigates this. But I see the issues getting worse in Perl6.

I'm certainly no expert on Perl6, but my understanding of this from the literature I've seen is that:

Which when you add it all up, means that clases like "scalar" and "array" can define the assignment operator to make a new copy of the rvalue and return that copy, while the assignment operator for whatever Abstract Base class all user defined classes subclass by default will probably have the assignment operator defined to return a reference to the rvalue.

(Assuming of course, that the Perl6 design folks want assignment of scalars/arrays/objects to work exactly the same way it does in Perl5 -- and even if they don't I'm sure Damian will write a "copyprmativesbyvalue" pragama that people can use.)

Replies are listed 'Best First'.
Re: Re: Perl6 - value vs. reference issues
by John M. Dlugosz (Monsignor) on Sep 16, 2002 at 20:05 UTC
    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