Re: vtables: Assignment vs. Aliasing

2001-02-11 Thread Simon Cozens

On Mon, Feb 05, 2001 at 03:35:18PM -0200, Branden wrote:
> It [aliasing] 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 

No, no, not at all. Aliasing is a feature of the symbol table.

Here's what's going on:

  Symbol Table  Data

PMC
  $a -->PMC
PMC
PMC
  $b -->PMC
PMC

When you want to do something with $a, you look up $a in the symbol table
and return its PMC, and operate on *that*. "$a" is just a name. It's not a
"thing".

In the case of aliasing:

  Symbol Table  Data

PMC
  $a -->PMC
  / PMC
 /  PMC
  $b ---/   PMC
PMC


-- 
Keep the number of passes in a compiler to a minimum.
-- D. Gries



Re: vtables: Assignment vs. Aliasing

2001-02-07 Thread Bart Lateur

[CC'ed to language, because I think it's there that it belongs]

On Mon, 5 Feb 2001 15:35:18 -0200, Branden wrote:

>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.

No way. Although I think aliasing is a great tool, but assignment is by
value. Always. (Well, except for referenced things...)

>In perl5 terms:
>*a = \$b;
>*a = \@b;  # or
>*a = \%b;

>However, typeglobs are said to disappear from Perl6,

I think Larry wants to drop typeglobs themselves, i.e. keeping different
kinds of variables of the same name in one record, but not the
possibilities they offer. Aliasing is likely the most interesting
feature of them all.

...

My preference:

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

I think this is a nice symmetrical syntax.

>* Make aliasing the default for = and provide another way of assigning (NO
>WAY!!!)

Indeed, no way.

Look, if you'd do the latter, you would not only make Perl effectively a
different language, but you'd also be missing out on one of the great
benefits of aliasing. For example, you pass a reference of a hash to a
sub, so the original hash can be accessed and modified. With the latter
syntax, you can't even do that through an alias. In the former syntax:

foo(\%bar);

sub foo {
my \%hash = shift;  # alias through reference
print $hash{FOO};
}

You can now access the passed hash as a hash, and not through the
slightly awkward syntax of accessing it through a reference:

sub foo2 {
my $hash = shift;
print $hash->{FOO};
}

(You don't think it's that awkward? Try getting a hash slice through a
hash reference. Ugh.)

-- 
Bart.



Re: vtables: Assignment vs. Aliasing

2001-02-05 Thread Simon Cozens

On Mon, Feb 05, 2001 at 03:35:18PM -0200, Branden wrote:
> How will this problem be solved, and, more generally, how will aliasing take
> place in perl6?

This is perhaps a bad way to answer the question, but:

I'm hoping that the vtable PDD should be around in a few days (Pssst, Dan!)
and it might be worth holding onto your questions until you've had a look at
it, as it will probably give you a much clearer idea of what's going to be
going on than any amount of discussion. 

That doesn't mean that the PDD will cover aliasing, (as far as I know, it
doesn't yet) but it does mean that once you've seen how vtables work, you
might have a much better idea of how aliasing can be possible, and we'll
all be a lot better *equipped* for this sort of conversation.

-- 
Sometimes when you fill a vacuum, it still sucks.   -Rob Pike



vtables: Assignment vs. Aliasing

2001-02-05 Thread Branden

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
WAY!!!)

* 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