Assignment to nested lists in Perl 6

  • A+
Category:Languages

I supposed, the result should be 1, 2, 3.

> my ($a, $b, $c) > (($a, $b), $c) = ((1, 2), 3) (((1 2) 3) (Any)) > $a, $b, $c ((1 2) 3 (Any)) 

What's wrong here?


There's nothing wrong (that is, ordinary assignment in P6 is designed to do as it has done) but at a guess you were hoping that making the structure on the two sides the same would result in $a getting 1, $b getting 2 and $c getting 3.

For that, you want "binding assignment" (aka just "binding"), not ordinary assignment:

my ($a, $b, $c); :(($a, $b), $c) := ((1, 2), 3); 

Note the colon before the list on the left, making it a signature literal, and the colon before the =, making it a binding operation.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: