|
Perl 6
SMOP Multi Sub Dispatch: Revision 4
Multi subs in Perl 6 support two very nasty features:
* lexical variants of outer-or-global multi subs * variant disambiguation What do they mean? ^^ Lexical Variants .pre my multi foo (Foo $a) {...}; for 1..4 { my multi foo (Bar $a) {...}; # foo(Bar) is visible here } # but not here .pre ^^ Variant Disambiguation .pre class A {}; class B is A {}; multi foo (A $a) { say 1 }; multi foo (B $a) { say 2 }; foo(A.new()); foo(B.new()); .pre 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 work The dispatching would implement the following steps: * manual traversal of the lexical scope searching for variants: This is not a simple lookup because we need the variant declared in an inner scope not to hide the declaration in an outer scope. * check if package CANDO this sub: sub dispatch is 'no strict', meaning that if the symbol is not found in the current lexical scope, it might be in the package. * grep all the variants for ACCEPTS * sort them according to a "pluggable disambiguation code" * invoke the topmost variant on the sorted list |