Skip Menu |
Report information
Id: 127924
Status: rejected
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: cpan [at] zoffix.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [IMPROVEMENT] Explicit use of `return` on `is rw` subs should behave like `return-rw`
Currently, if you use explicit keyword `return` to return out of a sub marked with `is rw` trait, you still get a read-only value, even though you get a read-write one if you omit `return`: <ZoffixW> m: my %h = foo => 'bar'; sub lookup ($key) is rw { return %h{$key}; }; lookup('foo') = 'meow'; say %h; <camelia> rakudo-moar 1aabef: OUTPUT«Cannot assign to a readonly variable or a value␤ in block <unit> at /tmp/buNGUMJLYO line 1␤␤» To rectify this, `return-rw` keyword exists, BUT it doesn't require the sub to have `is rw` trait. Thus, the `is rw` trait's usefulness is narrowed down to only subs that rely on implicit returns. It also produces confusion, since an average programmer would expect explicit placement of keyword `return` to retain the program's behaviour unchanged. Thus, I propose that when a `return` is called from a sub or method marked with `is rw` trait, `return-rw` should be called instead. This makes the train more useful and its behavour more predictable. Relevant IRC discussion: http://irclog.perlgeek.de/perl6/2016-04-18#i_12356836
Download (untitled) / with headers
text/plain 1.3k
On Mon Apr 18 11:16:05 2016, cpan@zoffix.com wrote: Show quoted text
> Currently, if you use explicit keyword `return` to return out of a sub > marked with `is rw` trait, you still get a read-only value, even > though you get a read-write one if you omit `return`: > > <ZoffixW> m: my %h = foo => 'bar'; sub lookup ($key) is rw { return > %h{$key}; }; lookup('foo') = 'meow'; say %h; > <camelia> rakudo-moar 1aabef: OUTPUT«Cannot assign to a readonly > variable or a value␤ in block <unit> at /tmp/buNGUMJLYO line 1␤␤» > > To rectify this, `return-rw` keyword exists, BUT it doesn't require > the sub to have `is rw` trait. Thus, the `is rw` trait's usefulness is > narrowed down to only subs that rely on implicit returns. It also > produces confusion, since an average programmer would expect explicit > placement of keyword `return` to retain the program's behaviour > unchanged. > > Thus, I propose that when a `return` is called from a sub or method > marked with `is rw` trait, `return-rw` should be called instead. This > makes the train more useful and its behavour more predictable. > > Relevant IRC discussion: http://irclog.perlgeek.de/perl6/2016-04- > 18#i_12356836
FWIW, the Perl 5 return keyword works with both lvalue and non-lvalue subs. It checks the attribute on the sub and behaves accordingly. -- Father Chrysostomos
Closing due to lack of interest.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org