Skip to content

Instantly share code, notes, and snippets.

@jonathanstowe
Created January 13, 2016 22:51
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 jonathanstowe/c00b7a9a7709d502b404 to your computer and use it in GitHub Desktop.
Save jonathanstowe/c00b7a9a7709d502b404 to your computer and use it in GitHub Desktop.
#!perl6
role EnterValidator {}
role Blobject { }
class ObjectOne does Blobject {
}
class ObjectTwo does Blobject {
}
multi sub trait_mod:<is> ( Method $m, :$enter-validator! ) {
$m does EnterValidator;
}
class Foo {
method any(Blobject $blob) returns Bool is enter-validator { say "any";}
method one(ObjectOne $blob) returns Bool is enter-validator { say "one"; }
method two(ObjectTwo $blob) returns Bool is enter-validator { say "two"; }
method run-em(Blobject $object) {
for self.^methods.grep(EnterValidator) -> $meth {
if $object.WHAT ~~ $meth.signature.params[1].type {
self.$meth($object);
}
}
}
}
my $foo = Foo.new;
say "running one";
$foo.run-em(ObjectOne.new);
say "running two";
$foo.run-em(ObjectTwo.new);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment