Here's the program:
my %SUB-COUNTS;
say "Start";
multi sub trait_mod:<is>(Sub $s where .name().contains("-logged"), :$AOP) {
$s.wrap({
say "Entering { $s.name }";
callsame;
});
}
multi sub trait_mod:<is>(Sub $s where .name().contains("-counted"), :$AOP) {
$s.wrap({
say "Counting { $s.name }";
%SUB-COUNTS{$s.name}++;
});
}
sub water-logged() is AOP {
return "Water";
}
sub out-logged() is AOP {
return "Out";
}
sub we're-counted() is AOP {
state $count = 0;
return $count++;
}
sub we're-counted-and-logged() is AOP {
state $alpha = 'a';
return $alpha++;
}
say water-logged(), " ", out-logged(), " ", water-logged();
say we're-counted() for ^20;
say we're-counted-and-logged() for ^20;
say %SUB-COUNTS;
This works, but only 1 trait can be applied to every routine, so I've thought about using redispatch so that routines can be counted and logged. However, doing this:
multi sub trait_mod:<is>(Sub $s where .name().contains("-logged"), :$AOP) {
$s.wrap({
say "Entering { $s.name }";
callsame;
});
callsame;
}
Results in a weird compile error:
Can't use unknown trait 'is' -> 'AOP' in a sub declaration.
at /home/jmerelo/txt/docencia/presentaciones/aop-raku/code/point-cut.raku:23
expecting any of:
rw raw hidden-from-backtrace hidden-from-USAGE pure default
implementation-detail DEPRECATED inlinable nodal prec equiv
tighter looser assoc leading_docs trailing_docs
Line 23 is the first declaration that uses AOP
. So I'm kind of lost here. How come adding a single statement alter the syntax in such a way?