Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created September 15, 2015 14:16
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 jnthn/b291be5a7a18398849d4 to your computer and use it in GitHub Desktop.
Save jnthn/b291be5a7a18398849d4 to your computer and use it in GitHub Desktop.
multi trait_mod:<is>(Attribute:D $attr, :$chainymutable!) {
$attr does role {
method compose(Mu \package) {
my constant NO_ARG = Mu.new;
my $this_attr = self;
package.^add_method:
$this_attr.name.substr(2), # strip sigil
method (Mu \value = NO_ARG) {
if value =:= NO_ARG {
$this_attr.get_value(self)
}
else {
$this_attr.set_value(self, value);
self
}
}
nextsame();
}
}
}
class Point {
has $.x is chainymutable;
has $.y is chainymutable;
}
my $p = Point.new.x(42).y(69);
say $p.x;
say $p.y;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment