develooper Front page | perl.perl6.internals | Postings from February 2001

vtables: Assignment vs. Aliasing

Thread Next
February 5, 2001 09:34
vtables: Assignment vs. Aliasing
Message ID:
There are two possible things that could happen when you say:
    $a = $b;
    @a = @b;  # or
    %a = %b;

These two things are assignment and aliasing. Assignment happens when the
value of the rvalue is fetched (by a vtable correspondent FETCH operation),
and this value is stored in the lvalue (by a vtable STORE operation). For
arrays & hashes, the values are assigned (aliased?) one by one.

By contrast, aliasing happens when the lvalue begins to point to the same
thing that the rvalue. In perl5 terms:
    *a = \$b;
    *a = \@b;  # or
    *a = \%b;

It means that the behaviour of $a/@a/%a is felt on $b/@b/%b, and vice-versa,
so that they are both the same thing. In vtable terms, the vtable of
$b/@b/%b would get copied to $a/@a/%a and (I'm not sure here...) the data
pointed by $b/@b/%b would be copied by $a/@a/%a (but what if one of them
changes it??? what really happens???). The copying of the vtable has an
interesting side effect: tied/magic stuff is copied from b to a. This is
very interesting in returning a magic stuff from a sub. For example:

    sub magic {
        my @b;
        tie @b, 'MyClass', @_;
        return \@b;

    my *b = magic();
    # now @b is tied to 'MyClass'.

However, typeglobs are said to disappear from Perl6, and also, the syntax
above is *bad*, because the sub must return a reference instead of the real
thing, and the script must assign to the typeglob to use the array, which
isn't a very obvious thing, at first glance.

How will this problem be solved, and, more generally, how will aliasing take
place in perl6?

I see some possibilities:

* For the problem above, add a :autoalias (or something like that) attribute
to the sub. The code would look like
    sub magic :autoalias {
        my @b;
        tie @b, 'MyClass', @_;
        return @b;       # <<-- return an alias to @b, like *b or \@b...
    my @b = magic();     # <<-- aliased, not assigned...
    # now @b is tied to 'MyClass'.
  However wouldn't solve the general aliasing case.

* Adding a special alias operator, like =* (mnemonic: typeglobs), then
    $a =* $b;
    @a =* @b;
    %a =* %b;
  However wouldn't solve the problem above completely, the programmer would
have to remember to use =* when assigning magic() to a variable.

* Make aliasing available by external modules:
    use Alias;
    Alias::make_alias(@a, @b);    # @a is an alias of @b (???)

* Alias when assigning to a reference:
    \$a = \$b;
    \@a = \@b;
    \%a = \%b;

* Make aliasing the default for = and provide another way of assigning (NO

* Keep typeglobs

* A combination of them. I particularly like the :autoalias attribute
approach, because it's the most transparent one, and the `returning a tied
object' from a sub is, IMO, the more used case, besides autoloading of subs
and namespace importing/exporting. However it's not general and there should
be another form of aliasing.

I'd like to know:
* Is there any hole in the :autoalias thing? Is it feasible? Is it useful?
Is it worth?
* What's your opinion about which of the above methods to use for aliasing?
* Is there another method for aliasing I'm forgetting?
* Alias should copy the vtable of the rvalue to the lvalue on aliasing,
right? Then what should happen to the data? Copying how, same reference,
duplicate the value? Perhaps special vtable entry for handling the value
part when aliasing?

- Branden

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About