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

Accessor $.x for rw attribute returns immutable value instead of Scalar, if it contains an Array or Hash #5313

Open
p6rt opened this issue May 11, 2016 · 4 comments
Labels

Comments

@p6rt
Copy link

p6rt commented May 11, 2016

Migrated from rt.perl.org#128124 (status was 'open')

Searchable as RT128124$

@p6rt
Copy link
Author

p6rt commented May 11, 2016

From martin@senfdax.de

<masak> m​: class Strange { has $.env is rw; method reset($env) { $.env =
$env } }; my $s = Strange.new; my %env = (1 => 2, 3 => 4);
$s.reset(%env); $s.reset(%env)
<camelia> rakudo-moar 9c8989​: OUTPUT«Cannot assign to a readonly
variable or a value� in method reset at /tmp/plljs3r5Zt line 1� in
block <unit> at /tmp/plljs3r5Zt line 1��»

<ufobat> soo, it's a bug?
<masak> pretty sure it is, yes
<masak> care to submit it? :)

@p6rt
Copy link
Author

p6rt commented May 11, 2016

From @smls

Confirmed.
Also, the same issue occurs when an Array is used instead of a Hash.

Simpler demonstration​:

  class A {
  has $.x is rw;
  method a {
  $.x = [1, 2, 3];
  $.x = [1, 2, 3]; # Cannot assign to a readonly variable or a value
  }
  }
  A.new.a;

The problem does not occur when `$!x` is used instead of `$.x`.

Adding `dd` print statements, yields an interesting observation​:

  class A {
  has $.x is rw;
 
  method a {
  dd $!x; # Any $!x = Any
  dd $.x; # Any $!x = Any
 
  $.x = [1, 2, 3];
 
  dd $!x; # Array $!x = $[1, 2, 3]
  dd $.x; # $[1, 2, 3]
 
  $.x = [1, 2, 3]; # Cannot assign to a readonly variable or a value
  }
  }

  A.new.a;

Apparently, once the `$!x` attribute contains an Array or Hash instance, its public accessor `$.x` no longer returns the mutable item container of the attribute as it should, and instead returns the Array/Hash value directly.

@p6rt
Copy link
Author

p6rt commented May 11, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jun 12, 2016

From @smls

UPDATE​:

This only happens when the special `$.attr` accessor syntax (for accessing an attribute of `self`) is used.

When the verbose method-form accessor syntax (`self.attr` or `$obj.attr`) is used, the assignment works fine. In fact, that form is bugged in the opposite way​: It allows the assignment even if the attribute is *not* marked `is rw`, but that bug is tracked by the separate ticket #​128324.

@p6rt p6rt added the Bug label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant