S06: Pairs as lvalues

6 views
Skip to first unread message

Ingo Blechschmidt

unread,
Feb 26, 2005, 7:27:19 AM2/26/05
to perl6-l...@perl.org
Hi,

quoting http://dev.perl.org/perl6/synopsis/S06.html:
> Pairs can be used as lvalues. The value of the pair is the
> recipient of the assignment:
>
> (key => $var) = "value";
>
> When binding pairs, names can be used to "match up" lvalues
> and rvalues:
>
> (who => $name, why => $reason) := (why => $because, who => "me");

that's really convenient, but what will the following code do?
my $x = (a => 42); # $x is a Pair.
$x = 13; # Is $x now the Pair (a => 13) or
# the Int 13?

When executing the second line, $x is a Pair. And it is used as a
lvalue. So, according to S06, the assignment will only change the
value of the Pair, not the Pair itself.

But I doubt this is what most programmers will expect -- example:
sub foo (Any $x) {
$x = 13; # Won't always make $x an Int.
...;
}
my $pair = (a => 42);
foo($pair);

So, does this Pairs-as-lvalue rule only affect "real syntax-Pairs"?
If not, what would the sub unpairify in the following code have to
look like?
my $x = (a => 42);
unpairify($x, 13);
# $x is now the Int 13.

sub unpairify (Any $x is rw, Int $num) {
...; # fill in please
}

--Ingo

--
Linux, the choice of a GNU | self-reference, n. - See self-reference
generation on a dual AMD- |
Athlon! |

Uri Guttman

unread,
Feb 26, 2005, 9:33:43 AM2/26/05
to Ingo Blechschmidt, perl6-l...@perl.org
>>>>> "IB" == Ingo Blechschmidt <ibl...@web.de> writes:

IB> Hi,
IB> quoting http://dev.perl.org/perl6/synopsis/S06.html:


>> Pairs can be used as lvalues. The value of the pair is the
>> recipient of the assignment:
>>
>> (key => $var) = "value";
>>
>> When binding pairs, names can be used to "match up" lvalues

^^^^^^^


>> and rvalues:
>>
>> (who => $name, why => $reason) := (why => $because, who => "me");

note the binding := which is not the same as =. binding is similar to
aliasing. in the above case it matches the names and assigns the new
values accordingly. i am not sure what happens if you have more pairs on
one side or the other (does it behave as hashes would and just overwrite
the common keys and assign new ones as needed?)

IB> that's really convenient, but what will the following code do?
IB> my $x = (a => 42); # $x is a Pair.
IB> $x = 13; # Is $x now the Pair (a => 13) or
IB> # the Int 13?

$x is 13 now as you assigned it. to assign the value of the pair as an
lvalue i think you would do:

$x.value = 13 ;

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Ingo Blechschmidt

unread,
Feb 26, 2005, 9:49:08 AM2/26/05
to perl6-l...@perl.org
Hi,

Uri Guttman <uri <at> stemsystems.com> writes:
> note the binding := which is not the same as =. binding is
> similar to aliasing. in the above case it matches the names
> and assigns the new values accordingly.

that makes sense. But consider:

> >>>>> "IB" == Ingo Blechschmidt <iblech <at> web.de> writes:
> IB> quoting http://dev.perl.org/perl6/synopsis/S06.html:
> >> Pairs can be used as lvalues. The value of the pair is the
> >> recipient of the assignment:
> >>
> >> (key => $var) = "value";

# Note: Simple assignment, no binding!

Here, there's a simple assignment, no binding. Is this a typo then,
and it should be read as C<< (key => $var) := "value" >>? If yes,
everything's clear.

If not, then consider:
(key => $var) = "value"; # Example from S06
my $value = "value";
(key => $var) = $value; # RHS replaced by a variable,
# should make not difference.
my $pair = (key => 3);
$pair = $value; # Former LHS (a Pair) replaced
# by a Pair variable, should
# make no difference, too...
say $pair.value; # Will print "value"...


--Ingo

--
Linux, the choice of a GNU | Mathematicians practice absolute freedom.
generation on a dual AMD- | -- Henry Adams
Athlon! |

Luke Palmer

unread,
Feb 26, 2005, 6:03:21 PM2/26/05
to Ingo Blechschmidt, perl6-l...@perl.org
Ingo Blechschmidt writes:
> that's really convenient, but what will the following code do?
> my $x = (a => 42); # $x is a Pair.
> $x = 13; # Is $x now the Pair (a => 13) or
> # the Int 13?

It's the Int 13. Your example looks a lot like this one:

my $x = [ 1, 2, 3 ];
$x = 13;

And you could say that is an error because:

[ 1, 2, 3 ] = 13;

Is an error, but you'd be wrong. You see, in your example, the pair is
not "functioning as an lvalue". The variable is the thing that is the
lvalue, not the pair.

I belive you could get the pair as an lvalue if you did:

my (Pair $x) := (a => 42);
$x = 13;

Because the variable x is now *fundamentally* a pair; it has no
container, so to speak. But it would die, because you're trying to
change a constant value.

Luke

Ingo Blechschmidt

unread,
Feb 27, 2005, 4:33:36 AM2/27/05
to perl6-l...@perl.org
Hi,

Luke Palmer <luke <at> luqui.org> writes:


> Ingo Blechschmidt writes:
> > my $x = (a => 42); # $x is a Pair.
> > $x = 13; # Is $x now the Pair (a => 13) or
> > # the Int 13?
>

> You see, in your example, the pair is not "functioning as
> an lvalue". The variable is the thing that is the lvalue,
> not the pair.

ah! That clears it up! Thanks! :)


--Ingo

--
Linux, the choice of a GNU | When cryptography is outlawed, bayl bhgynjf
generation on a dual AMD- | jvyy unir cevinpl!
Athlon! |

Reply all
Reply to author
Forward
0 new messages