nqp icon indicating copy to clipboard operation
nqp copied to clipboard

Iterating on multiple values nqp doesn't get optional items right

Open tadzik opened this issue 12 years ago • 4 comments

Code:

my @arr := [1, 2, 3];

for @arr -> $a, $b? {
    say("$a, $b");
}

Expected result:

1, 2
3,

Actual result:

1,
2,
3,

tadzik avatar Sep 14 '13 12:09 tadzik

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

pmichaud avatar Sep 14 '13 16:09 pmichaud

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

coke avatar Oct 23 '13 01:10 coke

Note that

for @x -> $a, $b { }

does work, so it feels like the other form should do.

jnthn avatar Oct 23 '13 12:10 jnthn

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".

timo avatar Nov 27 '13 19:11 timo