Skip to content

Instantly share code, notes, and snippets.

@ChoHag
Created June 25, 2014 17:22
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 ChoHag/e1a910d6ac454aadb7b3 to your computer and use it in GitHub Desktop.
Save ChoHag/e1a910d6ac454aadb7b3 to your computer and use it in GitHub Desktop.
role SomeTrait {
method apply_handles($attr: Mu $pkg) {
say "apply_handles";
my $name = $attr.name;
my $accessor = $name.subst(/^../, '');
my $r = sub ($obj, |args) is rw {
say "$name accessor";
nextsame;
};
$pkg.HOW.find_method($pkg, $accessor).wrap($r);
}
method foo { say "fooing" }
}
multi trait_mod:<is>(Attribute $var, :$wtf!) {
die "Must have accessor" unless $var.has-accessor;
$var.set_rw;
say "456";
say $var;
$var does SomeTrait;
$var.foo;
}
class foo {
has $.x is wtf;
}
say "begin";
my $foo = foo.new;#(x => 32);
my $bar = foo.new(x => 32);
say $foo.x;
say $bar.x;
say "---";
$foo.x = 64;
say "...";
@ChoHag
Copy link
Author

ChoHag commented Jun 25, 2014

dies with:

456
Mu $!x
fooing
apply_handles
begin
$!x accessor
(Any)
$!x accessor
32

---
$!x accessor
Cannot assign to a readonly variable or a value
  in block  at foo.6:36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment