Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

185 lines (119 sloc) 5.53 KB
=begin pod
=TITLE class Signature
class Signature { ... }
A signature is a static description of the parameter list of a code object.
That is, it describes what and how many arguments you need to pass to it
for invocation.
Passing arguments to a signature I<binds> the arguments the parameters,
and (loosely speaking) to the signature.
=head1 Signature Literals
Signatures appear in parenthesis after subroutine and method names, on
blocks after a C<< -> >> or C<< <-> >> arrow, or as a separate term starting
with a colon.
sub f($x) { }
# ^^^^ signature
method x() { }
# ^^ signature
my $s = sub (*@a) { }
# ^^^^^ signature
for @list -> $x { }
# ^^ signature
my $sig = :($a, $b);
# ^^^^^^^^ signature
=head2 Parameter Separators
A signature consists of zero or more I<parameters>, separated by comma.
As an exception the first parameter may be followed by a colon instead of
a comma to mark the invocant of a method.
:($a, @b, %c) # comma-separted parameters
:($a: @b, %c) # first argument is the invocant
=head2 Type Constraints
Parameters can optionally have a type constraint (the default is L<Any>).
Anonymous parameters are fine too.
:(Int $a, Str $b) # type constraints
:($, @, %a) # two anonymous and a "normal" parameter
:(Int, Positional) # just a type is also fine (two parameters)
In addition to those I<nominal> types, addtional constraints can
be placed on parameters in the form of code blocks which must return
a true value to pass the type check
sub f(Real $x where { $x > 0 }, Real $y where { $y >= $x }) { }
In fact it doesn't need to be a code block, anything on the right of the
C<where>-block will be used to smart-match the argument against it.
So you can also write
multi fact(Int $ where 0) { 1 }
multi fact(Int $x) { $x * fact($x - 1) }
The first of those can be shortened to
multi fact(0) { 1 }
i.e., you can use a literal directly as a type and value constraint
on an anonymous parameter.
=head2 Slurpy Parameters
An array or hash parameter can be marked as I<slurpy> by a leading asterisk,
which means they can bind to an arbitrary amount of arguments (zero or more).
:($a, *@b) # at least one argument, @b is slurpy
:($a, @b) # two arguments, the second one must be Positional
=head2 Positional vs. Named
A parameter can be I<postional> or I<named>. All parameters are positional,
except those marked with a leading colon C<:>, and slurpy hash parameters.
:($a) # a positional parameter
:(:$a) # a named parameter of name 'a'
:(*@a) # a slurpy positional parameter
:(*%h) # a slurpy named parameter
On the caller side, positional arguments are passed in the same order
as the parameters were declared.
sub pos($x, $y) { "x=$x y=$y" }
pos(4, 5); # x=4 y=5
In the case of named arguments and parameters, only the name is used for
mapping arguments to parameters
sub named(:$x, :$y) { "x=$x y=$y" }
named( y => 5, x => 4); # x=4 y=5
It is possible to have a different name for a named parameter than the
variable name:
sub named(:official($private) { } # parameter name is 'official'
Aliases are also possible that way:
sub paint( :color(:colour($c)) ) # 'color' and 'colour' are both OK
sub paint( :color(:$colour) ) # same API for the caller
=head2 Optional and Mandatory Parameters
Named parameters are optional by default, and can be made mandatory with a
trailing exclamation mark:
:(:$name!) # mandatory 'name' named parameter
Positional parameters are mandatory by default, and can be made optional
with a default value or a trailing question mark:
:($base = 10) # optional parameter, default value 10
:(Int $x?) # optional parameter, default is the Int type object
Named parameters can also have default values.
Default values can depend on previous parameters, and are (at least
notionally) computed anew for each call
:($goal, $accuarcy = $goal / 100);
:(:$excludes = ['.', '..']); # a new Array for every call
=head2 Parameter Traits and Modifiers
By default, parameters are bound to their argument and marked as
read-only. One can change that with traits on the parameter.
The C<is copy> trait causes the argument to be copied, and allows it
to be modified inside the routine
sub c($x is copy) {
$x = Inf if $x ~~ Whatever;
.say for 1..$x;
The C<is rw> trait makes the parameter only bind to a variable (or other
writable container)N<this does not work yet in Rakudo>. Assigning to the
parameter changes the value of the variable at the caller side
sub swap($x is rw, $y is rw) {
($x, $y) = ($y, $x);
To bind either to a value or a variable, one can prefix a parameter with
a backslash C<\>.
sub f(\$raw) { ... }
Prefixing a parameter with a vertical bar C<|> makes it use up all the
remaining positional and named arguments.
=head1 Methods
=head2 params
method params(Signature:D:) returns Positional
Returns the list of L<Parameter> objects that make up the signature.
=head2 arity
method arity(Signature:D:) returns Int:D
Returns the minimal number of positional arguments required to satisfy
the signature.
=head2 count
method count(Signature:D:) returns Real:D
Returns the maximal number of positional arguments which can be bound
to the signature. Returns C<Inf> if there is a slurpy positional parameter.
=end pod
Jump to Line
Something went wrong with that request. Please try again.