Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

raku - Last element of a block thrown in sink context

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+= @^t} for @bitfields;

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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+= @^t} 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...