Search code examples
raku

Can't use sequence operator `...` on a separate line in Raku


The following code works:

my @fibo = 1, 1, -> $a, $b { $a + $b } ...  *;
say @fibo[^10];

output:

(1 1 2 3 5 8 13 21 34 55)

But if you split this over multiple lines like this:

my @fibo = 1,
           1,
           -> $a, $b { $a + $b }
           ...
           *;
say @fibo[^10];

it gives an error:

*
  in block <unit> at ./dotdotdot line 4

It appears that the ... is interpreted as the yada-operator in this case. But it shouldn't, should it? Both versions of the code should be equivalent. (This isn't Python, whitespace should be mostly irrelevant.)

As a workaround, the following does work correctly:

my @fibo = 1,
           1,
           -> $a, $b { $a + $b } ...
           *;
say @fibo[^10];

(Of course, this is a very artificial example, but I found this when using a much more complicated sequence that's too long to put on one line.)

Tested up to 2023.11.


Solution

  • Raku has a very simple unforgiving rule that if the last thing in a line of code is a block that would make sense as the end of a statement then it's the end of a statement. There are many solutions, such as the one you suggested, or adding a \ after the closing curly brace. -raiph