Last element of a block thrown in sink context

2019-04-29 03:39发布

This program

my @bitfields;
for ^3 -> $i {
    @bitfields[$i] = Bool.pick xx 3;
}

my @total = 0 xx 3;
for @bitfields -> @row {
    @total Z+= @row;
}
say @total;

says [0 0 0]. If we add something to the loop, whatever:

my @bitfields;
for ^3 -> $i {
    @bitfields[$i] = Bool.pick xx 3;
}

my @total = 0 xx 3;
for @bitfields -> @row {
    @total Z+= @row;
    say "foo";
}
say @total;

It will work correctly. Apparently, the last element of the block is thrown into sink context which in this case means it's simply ignored; this trap is related to that. However, that code above looks perfectly fine; and this

{@total Z+= @^þ} for @bitfields;

apparently works, although I don't see the real difference. Any other idea?

标签: perl6
1条回答
【Aperson】
2楼-- · 2019-04-29 04:19

It looks like a bug to me.

This looks very closely related to Which context confuses this Perl 6 zip operator? which became a Rakudo repo issue Failure to sink for when Z+= used as last statement which was closed with roast tests Test sunk for sinks last statement sub calls .

The mystery is why there's a new bug. My suspicion is that someone needs to clean the kitchen sink, i.e. pick up where Zoffix left off with his Flaws in implied sinkage / &unwanted helper issue.

Here's my best golf shot so far for narrowing down the new problem or regression:

my $foo = 'a';
ok:              for 1       { $foo X= 'b' }
notok:           for 1 -> $_ { $foo X= 'c' }
say $foo; # b
halfok: 'd' ~ do for 1 -> $_ { $foo X= 'e' } # Useless use of "~"
say $foo; # e

The ok: line works because it omits the -> argument.

The notok: line is my golf of your problem.

The error message for the halfok: line is because the result of it is thrown away. But the do has forced the compiler to evaluate the $foo X= 'e' expression in the block, as it should, and as it had failed to in the notok: line.

{@total Z+= @^þ} for @bitfields;

Perhaps that's because that's the non-modifier version. And/or because it doesn't use the -> syntax (which is part of the regression or new bug per my golf above).

Or perhaps just luck. I think most of the sink handling code in Rakudo is Larry's work from long ago when he was trying to get things mostly working right.

查看更多
登录 后发表回答