nqp
nqp copied to clipboard
Iterating on multiple values nqp doesn't get optional items right
Code:
my @arr := [1, 2, 3];
for @arr -> $a, $b? {
say("$a, $b");
}
Expected result:
1, 2
3,
Actual result:
1,
2,
3,
On Sat, Sep 14, 2013 at 05:24:31AM -0700, Tadeusz Sośnierz wrote:
Code:
my @arr := [1, 2, 3]; for @arr -> $a, $b? { say("$a, $b"); }
I'm not sure NQP is intended to support this construct.
Pm
From about here: http://irclog.perlgeek.de/perl6/2013-10-23#i_7750669
20:56 < tadzik> well, I asked jnthn about it on RaNIW, and his answer was "that should work". So, when it didn't, I opened a bug
Note that
for @x -> $a, $b { }
does work, so it feels like the other form should do.
I've been working on this issue in the "optional_for_arguments" branch. The trouble i'm having is this:
Currently i'm generating a series of "try to shift an element off", "if there was none, call the closure of the for loop with the current amount of arguments we shifted off". The problem with this is, that there is only one label that retry jumps to and at that point there would have to be a dispatcher that finds the correct invocation again, but that feels ugly to me.
@TimToady suggested to move the retry label into the closure that for calls, because it's not supposed to call the LEAVE queue when you retry, which would happen in this case. Even then, there'd have to be a bit of magic to preserve the values from the beginning of the closure for a retry.
On the other hand, making the code work like it currently does will give a better error message if you have
for @x -> $a, $b { }
and the array is odd-sized. Previously it would say "unable to shift from empty array", now it gives something like "required parameter $b not passed".