Created
October 27, 2015 22:54
-
-
Save anonymous/fbbf8440a94f531e89a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[jdv@wieldy ~]$ cat test_is_w1.p6 | |
use v6; | |
role WriteOnce { | |
method compose($pkg) { | |
callsame; | |
$pkg.^find_method(self.name.substr(2)).wrap(-> $obj { | |
Proxy.new( | |
FETCH => -> $ { self.get_value($obj) }, | |
STORE => -> $, $v { | |
die "Cannot re-set a set-once attribute" | |
if self.get_value($obj).defined; | |
self.set_value($obj,$v); | |
} | |
); | |
}); | |
} | |
} | |
multi sub trait_mod:<is>(Attribute:D $attr, :$set-once!) { | |
$attr does WriteOnce; | |
$attr.set_rw(); | |
warn "useless use of 'is set-once' on $attr.name()" | |
unless $attr.has_accessor; | |
} | |
class A { has $.foo is set-once } | |
my $a = A.new; | |
say $a.foo; | |
say $a.foo = 123; | |
{ say $a.foo = 456; CATCH { default { say $_; } } } | |
say $a.foo; | |
[jdv@wieldy ~]$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment