Skip to content

Commit 9a963ae

Browse files
committed
Document is deafult for Subs
Added explinations and examples to document what `is default` does for Subs.
1 parent 2447134 commit 9a963ae

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

doc/Type/Sub.pod6

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,26 @@ to it (or even themselves) and we can apply traits to objects at runtime.
6161
&trait_mod:<is>(&bar, :foo);
6262
# OUTPUT«is foo called␤is foo called␤»
6363
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+
6486
=end pod

0 commit comments

Comments
 (0)