> On 25 Sep 2015, at 12:05, 宋文泰 (via RT) <[email protected]> wrote:
> 
> # New Ticket Created by  宋文泰 
> # Please include the string:  [perl #126176]
> # in the subject line of all future correspondence about this issue. 
> # <URL: https://rt.perl.org/Ticket/Display.html?id=126176 >
> 
> 
> ``` perl6 code
> my $lastval = 4;
> sub lastval () is rw { return $lastval }
> dd $lastval;
> lastval() = 5;
> dd $lastval; 
> ```
> 
> Int $lastval = 4
> Cannot assign to a readonly variable or a value   
> in block  at t3.p6:7
> 
> This is perl6 version 2015.09-115-g4653d98 built on MoarVM version 
> 2015.09-36-g51dcbe4

Unfortunately, “is rw” doesn’t (yet anyway) make a left value subroutine work.  
You need it to return a Proxy object as well:

my $lastval;
sub lastval() is rw {
    Proxy.new(
      FETCH => { $lastval },                         # as a RHS, like $a = 
lastval;
      STORE => -> $, $value { $lastval = $value },   # as a LHS, like lastval() 
= $a
    )
}
lastval() = 42;
say $lastval;


At one point Perl 6 may develop syntactic sugar for this, but for now this is 
the way to do this.


Ticket can be closed as NotABug


Liz

Reply via email to