In Routine§
See primary documentation in context for trait is default
multi trait_mod:<is>(Routine:D $r, :$default!)
There is a special trait for Routine
s called is default
. This trait is designed as a way to disambiguate multi
calls that would normally throw an error because the compiler would not know which one to use. This means that given the following two Routine
s, the one with the is default
trait will be called.
multi f() is default { say "Hello there" } multi f() { say "Hello friend" } f(); # OUTPUT: «"Hello there"»
The is default
trait can become very useful for debugging and other uses but keep in mind that it will only resolve an ambiguous dispatch between two Routine
s of the same precedence. If one of the Routine
s is narrower than another, then that one will be called. For example:
multi f() is default { say "Hello there" } multi f(:$greet) { say "Hello " ~ $greet } f(); # "Use of uninitialized value $greet..."
In this example, the multi
without is default
was called because it was actually narrower than the Sub
with it.