Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku confusing postfix ,= operator behavior

Tags:

operators

raku

This raku documentation page says operator ,= is supposed to concatenate

the contents of the variable on the left hand side and the expression on the right hand side

in a class-dependent way. The documentation also provides an example with a Hash variable:

my %a = :11a, :22b;
%a ,= :33x;
say %a # OUTPUT: «{a => 11, b => 22, x => 33}␤»

This works for me, but when I try to do the same for an Array something confusing happens:

my @a = 1, 2;
@a ,= 3;
say @a 

And the output is something like (\Array_94393785458840 = [Array_94393785458840 3]). I would expect however the result be [1, 2, 3].

My question is: Is this the expected behavior? And if so, what is the purpose of such an operation?

like image 337
gmoshkin Avatar asked Feb 03 '20 15:02

gmoshkin


2 Answers

foo ,= bar

is short for

foo = foo, bar

So,

@a ,= 3

is short for:

@a = @a, 3;

During the Great List Refactor in 2015, the single argument rule became active everywhere. Since the right hand side of the expression is not a single argument, the @aon the right hand side, will not get flattened.

So effectively, you're creating a self-referential Array, in which the first element refers to itself (which is what the say is trying to tell you).

For reasons I'm not too clear about anymore, the same does not apply to objects doing the Associative role. So in that case, the %a does get flattened, thereby adding the given Pair to the Hash.

my %a = :11a, :22b;
%a = %a, :33x;
say %a # OUTPUT: «{a => 11, b => 22, x => 33}␤»

To get back to your question: in the case of an Array, I don't think there is much use for the ,= operation. Before the Great List Refactor, it may have done what you expected. But it would have been inconsistent with other situations, and consistency was deemed more important (because before the Great List Refactor, when something would flatten and when not, was rather arbitrary and many times a source of WAT instead of DWIM).

like image 174
Elizabeth Mattijsen Avatar answered Oct 10 '22 05:10

Elizabeth Mattijsen


for those less familiar with raku, it is easy to flatten any Array with a slip ‘|’ like so ... @a = |@a, :33; (at the cost of an extra operator)

like image 41
p6steve Avatar answered Oct 10 '22 03:10

p6steve