Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created March 11, 2012 18:22
Show Gist options
  • Save jnthn/2017537 to your computer and use it in GitHub Desktop.
Save jnthn/2017537 to your computer and use it in GitHub Desktop.
use PREPOSTSubmethods;
class C {
submethod PRE() {
say "in pre";
True
}
method foo() {
say 42;
}
submethod POST() {
say "in post";
False
}
}
C.foo();
module EXPORTHOW {
class class is Metamodel::ClassHOW is Mu {
has $!has-pre;
has $!has-post;
method add_method(Mu $obj, $name, $meth) {
callsame;
if $meth ~~ Submethod {
$!has-pre = 1 if $name eq 'PRE';
$!has-post = 1 if $name eq 'POST';
}
}
method compose(Mu $obj) {
if $!has-pre || $!has-post {
for self.methods($obj) {
next if .isa(Submethod);
.wrap(-> $obj, |$args {
$!has-pre and $obj.PRE() || die "Precondition failed";
my $result := callsame;
$!has-post and $obj.POST() || die "Postcondition failed";
$result
});
}
}
nextsame;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment