6

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?

1 Answer 1

15

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.

2
  • 2
    Very nice; something that seemed off suddenly makes complete sense :-)
    – ozzy
    Feb 27, 2019 at 10:19
  • 2
    It's why perl6 -e can often be better than the REPL for testing stuff. Feb 27, 2019 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.