Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to an immutable value #11

Open
zostay opened this issue Mar 13, 2019 · 3 comments
Open

Cannot assign to an immutable value #11

zostay opened this issue Mar 13, 2019 · 3 comments

Comments

@zostay
Copy link
Owner

zostay commented Mar 13, 2019

This may be a transient error in testing. I didn't notice this one until after I fixed the other initializer test failures. It may have been layered on top of previous failures that I was just ignoring while I had a known stupidity failure in the way I wrote the original tests.

@zostay
Copy link
Owner Author

zostay commented Mar 13, 2019

This proof of concept demonstrates the problem.

use v6;
use ArrayHash;

my $b = 2;
my %hash := ArrayHash.new('a' => 1, 'b' => $b, 'c' => 3);
%hash<a> = 4;

This appears to be a regression that started with 2018.10.

@zostay
Copy link
Owner Author

zostay commented Mar 13, 2019

Some investigation shows that the problem is basically that before 2018.10, a Pair created like 'a' => 1 contained a Scalar container for the value. At some point between 2018.06 and 2018.10 rakudo releases, this was changed so that 'a' => 1 lacks a Scalar and points to a bare Int (i.e., is immutable). The hash version of this assignment does provide a Scalar in every case. We can see this in the following one-liners:

% perl -e 'my @array-of-pairs = "a" => 1; @array-of-pairs[0].value = 2; dd @array-of-pairs'
Cannot modify an immutable Int (1)
  in block <unit> at -e line 1

and

% perl -e 'my %hash = "a" => 1; %hash<a> = 2; dd %hash'
Hash %hash = {:a(2)}

I'm not sure what the justification for this difference in behavior is, but I'm assuming to this point this change is intentional and has a good justification. In any case, this appears to be a feature, not a bug. The test needs to be fixed, not the module.

Weirdly, I would have expected the following modification to the @array-of-pairs one-liner to work:

% perl -e 'my @array-of-pairs = "a" => $(1); @array-of-pairs[0].value = 2; dd @array-of-pairs'
Cannot modify an immutable Int (1)
  in block <unit> at -e line 1

However, it most certainly does not. The following modification does work, though:

% perl6 -e 'my @array-hash = "a" => $=1; @array-hash[0].value = 2; dd @array-hash'
Array @array-hash = [:a(2)]

I propose to fix this by adding anonymous scalars into the initializers for array-hash.t and multi-hash.t. Bleh.

@zostay
Copy link
Owner Author

zostay commented Mar 13, 2019

Upon further reflection, I think I want to lean towards the hash treatment. This is an ArrayHash after all. I need to think more about how to make the interface consistent, but not make this unexpected distinction between positional pairs and named pairs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant