Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku pop() order of execution

Tags:

raku

Isn't order of execution generally from left to right in Raku?

my @a = my @b = [9 , 3];
say (@a[1] - @a[0]) == (@b[1] R- @b[0]); # False {as expected}
say (@a.pop() - @a.pop()) == (@b.pop() R- @b.pop()); # True {Huh?!?}

This is what I get in Rakudo(tm) v2020.12 and 2021.07. The first 2 lines make sense, but the third I can not fathom.

like image 916
FyFAIR Avatar asked Aug 12 '21 17:08

FyFAIR


1 Answers

It is.

But you should realize that the minus infix operator is just a subroutine under the hood, taking 2 parameters that are evaluated left to right. So when you're saying:

$a - $b

you are in fact calling the infix:<-> sub:

infix:<->($a,$b);

The R meta-operator basically creates a wrap around the infix:<-> sub that reverses the arguments:

my &infix:<R->($a,$b) = &infix:<->.wrap: -> $a, $b { nextwith $b, $a }

So, if you do a:

$a R- $b

you are in fact doing a:

infix:<R->($a,$b)

which is then basically a:

infix:<->($b,$a)

Note that in the call to infix:<R-> in your example, $a become 3, and $b becomes 9 because the order of the arguments is processed left to right. This then calls infix:<->(3,9), producing the -6 value that you would also get without the R.

It may be a little counter-intuitive, but I consider this behaviour as correct. Although the documentation could probably use some additional explanation on this behaviour.

like image 184
Elizabeth Mattijsen Avatar answered Jan 03 '23 18:01

Elizabeth Mattijsen