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

CStructs do not support Str is rw Attributes #2589

Closed
Xliff opened this issue Jan 7, 2019 · 1 comment
Closed

CStructs do not support Str is rw Attributes #2589

Xliff opened this issue Jan 7, 2019 · 1 comment

Comments

@Xliff
Copy link
Contributor

Xliff commented Jan 7, 2019

The Problem

This should create a CStruct backed object with a string attribute that allows changing.

class A is repr('CStruct') { 
   has Str $.s is rw; 
}
my $a = A.new( s => 'Hello' );

But instead, this occurs:

rakudo-moar 6d58e0b: OUTPUT: «Cannot assign to an immutable value␤ in block at line 1␤␤»

Expected Behavior

my $a = A.new( s => 'Hello' );

Should create a new object $a with $a.s containing the string 'Hello';

For easy golfing, the entire string used to illustrate the problem is:

m: class A is repr('CStruct') { has Str $.s is rw; }; my $a = A.new( s => 'Hello' );

Oddly enough, the solution involves using nqp;

class A is repr('CStruct') { 
  has Str $!s; 

  method s is rw { 
    use nqp; 
    Proxy.new: 
      FETCH => -> $ {$!s}, 
      STORE => -> $, $new { nqp::bindattr( nqp::decont(self), A, '$!s', nqp::unbox_s(~$new) ) } 
  }
}
my $a = A.new; $a.s = Hello'; 
$a.s.say

Again, this can be replicated in IRC using:

m: class A is repr('CStruct') { has Str $!s; method s is rw { use nqp; Proxy.new: FETCH => -> $ {$!s}, STORE => -> $, $new { nqp::bindattr( nqp::decont(self), A, '$!s', nqp::unbox_s(~$new) ); }; }; }; my $a = A.new; $a.s = 'Hello'; $a.s.say

This trick was discovered by looking through old code from FROGGS in p6-XML-LibXML.

Environment

This was found on the #perl6 IRC bot camelia.
rakudo (2018.12.165.g.6.d.58.e.0.b.0.b)

@Xliff
Copy link
Contributor Author

Xliff commented May 27, 2019

After talking to tadzik++ in IRC, I have learned about the "str" native variant of Str, which works as expected.

There is still an issue of assigning CPointer members using "is rw", but that is beyond the scope of this error.

Closing.

@Xliff Xliff closed this as completed May 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants