brian d foy
brian d foy

Reputation: 132896

Which context confuses this Perl 6 zip operator?

Consider this program where I create a hash. I want to then change two values in it:

my $hash = %(
    wallet   => 100,
    gave     =>   0,
    received =>   0,
    );

for ^1 { $hash<wallet gave> Z+= <-1 1> };

dd $hash;

Like this, the last line of for doesn't do anything and there is no warning. The hash is unchanged:

Hash $hash = ${:gave(0), :received(0), :wallet(100)}

Adding another statement changes the behavior:

my $hash = %(
    wallet   => 100,
    gave     =>   0,
    received =>   0,
    );

for ^1 { $hash<wallet gave> Z+= <-1 1>; True };

dd $hash;

Now the inplace edit does its thing, but there's a warning (although I dispute "useless" when I've found a use for it):

Useless use of constant value True in sink context
Hash $hash = ${:gave(1), :received(0), :wallet(99)}

If I do without the Z+=, which should be the same thing, it works:

my $hash = %(
    wallet   => 100,
    gave     =>   0,
    received =>   0,
    );

for ^1 { $hash<wallet gave> = $hash<wallet gave> Z+ <-1 1> }

dd $hash;

Again the right output:

Hash $hash = ${:gave(1), :received(0), :wallet(99)}

Upvotes: 9

Views: 285

Answers (1)

user2410502
user2410502

Reputation:

It's a bug. Fixed as of Rakudo 2018.02.1-45-g8a10fc1

Upvotes: 1

Related Questions