File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -61,4 +61,26 @@ to it (or even themselves) and we can apply traits to objects at runtime.
61
61
&trait_mod:<is>(&bar, :foo);
62
62
# OUTPUT « is foo calledis foo called »
63
63
64
+ There is a special trait for C < Sub > s called C < is default > . This trait is
65
+ designed as a way to disambiguate C < multi > function calls that would normally
66
+ throw an error because Rakudo would not know which one to use. This means that
67
+ given the following two C < Sub > s, the one with the C < is default > trait will be
68
+ called.
69
+
70
+ multi sub f() is default { say "Hello there" }
71
+ multi sub f() { say "Hello friend" }
72
+ f(); #"Hello there" is printed.
73
+
74
+ The C < is default > trait can become very useful for debugging and other uses but
75
+ keep in mind that it will only resolve an ambiguous dispatch between two C < Sub > s
76
+ of the same precedence. If one of the C < Sub > s are narrower than another, then
77
+ that one will be called. For example:
78
+
79
+ multi sub f() is default { say "Hello there" }
80
+ multi sub f(:$greet) { say "Hello " ~ $greet }
81
+ f(); #"Use of uninitialized value $greet..."
82
+
83
+ In this example, the C < multi > without C < is default > was called because it was
84
+ actually narrower than the C < Sub > with it.
85
+
64
86
= end pod
You can’t perform that action at this time.
0 commit comments