I'm assuming here you're asking about why there's no warning with respect to the stubbing. Indeed, normally a stubbed method must be implemented — but that's it.
You can see how the roles are composed into class here in Rakudo's source ($yada
basically means $is-stubbed
):
if $yada {
unless has_method($!target, $name, 0)
|| $!target.HOW.has_public_attribute($!target, $name) {
my @needed;
for @!roles {
for nqp::hllize($_.HOW.method_table($_)) -> $m {
if $m.key eq $name {
nqp::push(@needed, $_.HOW.name($_));
}
}
}
nqp::push(@stubs, nqp::hash('name', $name, 'needed', @needed, 'target', $!target));
}
}
So you can see that the logic is just to see if a method exists with the same name. It's definitely possible to write a module that would update this logic by augmenting the apply() method or outright replacing the RoleToClassApplier
class. However, it might be difficult. For example, consider:
class Letter { }
class A is Letter { }
class B is Letter { }
role Foo {
method foo (Letter) { ... }
}
class Bar does Foo {
method foo (A) { 'a' }
method foo (B) { 'b' }
}
Should we consider the stubbed method to be correctly implemented? But someone else could later say class C is Letter
and suddenly it's not implemented. (Of course, we could say that the best logic would be require, at the least, the identical or supertype, but that's more restictive than necessary for dynamic languages, IMO).
There isn't, AFAICT, a method that's called on roles during composition that also references the class (actually, there isn't any method called at all in add_method
), so there's no way to write your own check within the role . (but I could be wrong, maybe raiph, lizmat or jnthn could correct me).
My recommendation in this case would be to, instead of stubbing it, simply add in a die statement:
role L {
method do-l(Int $a, Int $b --> Int) {
die "SORRY! Classes implementing role L must support the signature (Int, Int)";
}
}
This won't capture it at compilation, but if at any point another method in L
needs to call on do-l(Int, Int)
—even if the composing class implements other signatures—, it will get called and the error caught fairly quickly.