Skip to content

Instantly share code, notes, and snippets.

Created October 27, 2015 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/6e0a148a351667b5d345 to your computer and use it in GitHub Desktop.
Save anonymous/6e0a148a351667b5d345 to your computer and use it in GitHub Desktop.
[jdv@wieldy ~]$ cat test_is_w1.p6
use v6;
role WriteOnce {
method compose($pkg) {
callsame;
$pkg.^find_method(self.name.substr(2)).wrap(-> $obj {
say "JDV1:",$obj.perl;
say "JDV2:",self.get_value($obj).perl;
die "Cannot assign more than once to a writeonce attribute"
if self.get_value($obj).defined;
callsame;
});
}
}
multi sub trait_mod:<is>(Attribute:D $attr, :$w1!) {
$attr does WriteOnce;
$attr.set_rw();
warn "useless use of 'is w1' on $attr.name()" unless $attr.has_accessor;
}
class A { has $.foo is w1 }
say A.new.foo;
say A.new.foo = 123; # OK
say A.new.foo = 456; # should die
[jdv@wieldy ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment