|
Perl 6
SMOP Multi Sub Dispatch: Revision 6
Multi subs in Perl 6 support two features:
What do they mean? Lexical Variants
my multi foo (Foo $a) {...};
for 1..4 {
my multi foo (Bar $a) {...};
# foo(Bar) is visible here
}
# but not here
Variant Disambiguation
class A {};
class B is A {};
multi foo (A $a) { say 1 };
multi foo (B $a) { say 2 };
foo(A.new());
foo(B.new());
Most languages that support multi dispatch doesn't support this feature, this is usually seen as a compile-time error, since the two variant are ambiguous. But this is a very important feature to Perl 6, since that's how objects can overload most operators. Which means you need a disambiguation code to sort all the matching candidates. How to make it workThe dispatching would implement the following steps:
API
module P6SubDispatch {
only sub dispatch ($name, $lexpad, $package) {...}
}
role P6VariantDisambiguator {
only sub sort (*@variants) {...}
}
|