Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 REPL print behaviour

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?

like image 558
ozzy Avatar asked Feb 27 '19 08:02

ozzy


1 Answers

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.

like image 142
Elizabeth Mattijsen Avatar answered Sep 28 '22 16:09

Elizabeth Mattijsen