When I execute the following statement in the Perl6 REPL:
my $var = 1, 2, 3;
it prints:
(1 2 3)
This seems curious to me, because $var is actually assigned a single integer (i.e. $var.WHAT returns (Int)), rather than a List of Ints.
I take it that the reason that an Int is assigned is the higher precedence of the item assignment operator (=) relative to the comma operator (,), which leaves the ,2,3 in sink context. But why does the REPL display a List of Ints? And what does the REPL in general display after the execution of a statement?
The REPL basically does a say (my $var = 1,2,3). Because the result of that expression is a List, it will show as (1 2 3). Inside that expression, only the first element of that list gets assigned to $a, hence it being an Int.
So why didn't it warn about that? This does, as you pointed out:
$ perl6 -e 'my $a = 1,2,3'
WARNINGS for -e:
Useless use of constant integer 2 in sink context (lines 1, 1)
Useless use of constant integer 3 in sink context (lines 1, 1)
whereas this doesn't:
$ perl6 -e 'say (my $a = 1,2,3)'
(1 2 3)
The reason is simple: because of the say, the ,2,3 are no longer in sink context, as they are being used by the say.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With