perl6/doc/lib/Parameter
doc src
(title) class Parameter
class Parameter { }
Represents a parameter, for purpose of introspection.
The usual way to obtain a Parameter object is to create a signature,
and call .params on it to obtain a list of the Parameters.
my $sig = :(Str $x);
my $param = $sig.params[0];
say $sig.type; # Str()
See L<Signature> for more information, and also for an explanation
on what most of the concepts related to parameters mean.
Methods
(head2) name
Returns the variable name.
(head2) constraints
Returns additional constraints on the parameter (usually as an
all -Junction).
(head2) type
Returns the nominal type constraint of the paramter.
(head2) named
Returns True if it's a named parameter.
(head2) named_names
Returns a list of names/aliases for this parameter.
(head2) positional
Returns True if the parameter is positional.
(head2) slurpy
Returns True for slurpy parameters.
(head2) optional
Returns True for optional parameters.
(head2) parcel
Returns True for parcel parameters.
sub f(\$parcel) {
$parcel = 5;
}
f(my $x); # works
f(42); # dies in the assignment
Parcel parameters bind either a variable or a value passed to it, with
no decontainerization happen. That means that if a variable was passed
to it, you can assign to the parameter. This is different from
L<rw|#rw>-parameter which can only bind to variables, never to values.
(head2) capture
Returns True for parameters that capture the rest of the argument list.
sub f(\capture) { }
Capture parameters do not force any context on the values passed bound
to them, which is why they cannot have sigils.
(head2) rw
Returns True for is rw parameters.
(head2) copy
Returns True for is copy parameters.
(head2) readonly
Returns True for read-only parameters (the default).
(head2) invocant
Returns True if the parameter is the invocant parameter.
(head2) default
Returns a closure that upon invocation returns the default value for
this parameter, or Any if no default was provided.
(head2) type_captures
Returns a list of variable names of type captures associated with this
parameter.
sub a(::T ::U $x) { }
say &a.signature.params[0].type_captures; # T U