Skip to content

Instantly share code, notes, and snippets.

Created October 27, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fbbf8440a94f531e89a0 to your computer and use it in GitHub Desktop.
Save anonymous/fbbf8440a94f531e89a0 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 {
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