Skip to content

Commit

Permalink
Moves is default with the other traits #2824
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed May 24, 2019
1 parent 0ad9db1 commit 99dea0c
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions doc/Type/Routine.pod6
Expand Up @@ -21,33 +21,6 @@ on the C<anon> declarator|/language/variables#The_anon_declarator> for more
information.
X<|is default (Routine)>
=head2 X<Trait is default>
There is a special trait for C<Routine>s called C<is default>. This trait is
designed as a way to disambiguate C<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 C<Routine>s, the one with the C<is default> trait will
be called.
multi sub f() is default { say "Hello there" }
multi sub f() { say "Hello friend" }
f(); # OUTPUT: «"Hello there"␤»
The C<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
C<Routine>s of the same precedence. If one of the C<Routine>s is narrower than
another, then that one will be called. For example:
=begin code
multi sub f() is default { say "Hello there" }
multi sub f(:$greet) { say "Hello " ~ $greet }
f(); # "Use of uninitialized value $greet..."
=end code
In this example, the C<multi> without C<is default> was called because it was
actually narrower than the C<Sub> with it.
=head1 Methods
=head2 method name
Expand Down Expand Up @@ -358,6 +331,36 @@ TODO: explain export tags
=end comment
X<|is default (Routine)>
=head2 trait is default
Defined as
multi sub trait_mod:<is>(Routine:D $r, :$default!)
There is a special trait for C<Routine>s called C<is default>. This trait is
designed as a way to disambiguate C<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 C<Routine>s, the one with the C<is default> trait will
be called.
multi sub f() is default { say "Hello there" }
multi sub f() { say "Hello friend" }
f(); # OUTPUT: «"Hello there"␤»
The C<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
C<Routine>s of the same precedence. If one of the C<Routine>s is narrower than
another, then that one will be called. For example:
=begin code
multi sub f() is default { say "Hello there" }
multi sub f(:$greet) { say "Hello " ~ $greet }
f(); # "Use of uninitialized value $greet..."
=end code
In this example, the C<multi> without C<is default> was called because it was
actually narrower than the C<Sub> with it.
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 comments on commit 99dea0c

Please sign in to comment.