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