Created
March 11, 2012 18:22
-
-
Save jnthn/2017537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use PREPOSTSubmethods; | |
class C { | |
submethod PRE() { | |
say "in pre"; | |
True | |
} | |
method foo() { | |
say 42; | |
} | |
submethod POST() { | |
say "in post"; | |
False | |
} | |
} | |
C.foo(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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