Parameter Reference
Element of a Signature
What it is #
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method name #
method name(Parameter:D: --> Str:D)
Returns Str:D.
Returns the parameter name, which includes all sigils and twigils. This name is used internally when applied to code, or in a declaration to determine the declared the name. This name is not necessarily usable by a caller – if it is, it will also appear as an alias. Often, the name will be chosen descriptively as a form of self-documentation.
method usage-name #
method usage-name(Parameter:D: --> Str:D)
Returns Str:D.
Returns the parameter name without any sigils and twigils. If the parameter is anonymous, an empty string will be returned. Note: Before Rakudo release 2020.08 the return value for an anonymous parameter was Nil.
method sigil #
method sigil(Parameter:D: --> Str:D)
Returns Str:D.
Returns a string containing the parameter's sigil, for a looser definition of "sigil" than what is considered part of the parameter's name. Still returns a sigil even if the parameter is anonymous. This "sigil" is actually an introspection used to help determine the normal binding style of a parameter, if it has not been altered through a trait.
method type #
Returns the nominal type constraint of the parameter.
method coerce_type #
Returns the coercion type of the parameter.
method constraints #
Returns additional constraints on the parameter (usually as an all-Junction).
method named #
method named(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True if it's a named parameter.
method named_names #
method named_names(Parameter:D: --> List:D)
Returns List:D.
Returns the list of externally usable names/aliases for a named parameter.
method positional #
method positional(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True if the parameter is positional.
method slurpy #
method slurpy(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for slurpy parameters.
method twigil #
method twigil(Parameter:D: --> Str:D)
Returns Str:D.
Returns a string containing the twigil part of the parameter's name.
method optional #
method optional(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for optional parameters.
method raw #
method raw(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for raw parameters. Raw parameters bind either a variable or a value passed to it, with no decontainerization taking place. That means that if a variable was passed to it, you can assign to the parameter. This is different from rw-parameter which can only bind to variables, never to values. This is the normal behavior for parameters declared with a sigil of '\', which is not really a sigil insofar
method capture #
method capture(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for parameters that capture the rest of the argument list into a single Capture object. Like raw parameters, Capture parameters do not force any context on the values bound to them, which is why their sigils are only used in declarations.
method rw #
method rw(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for is rw parameters.
method copy #
method copy(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for is copy parameters.
method readonly #
method readonly(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True for read-only parameters (the default).
method invocant #
method invocant(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True if the parameter is the invocant parameter.
method multi-invocant #
method multi-invocant(Parameter:D: --> Bool:D)
Returns Bool:D.
Returns True if the parameter affects multi dispatch.
method default #
method default(Parameter:D: --> Code:_)
Returns Code:_.
Returns a closure that upon invocation returns the default value for this parameter, or Code if no default was provided. Note: Before Rakudo release 2020.08 the return value for a parameter with no default value was Any.
method type_captures #
method type_captures(Parameter:D: --> List:D)
Returns List:D.
Returns a list of variable names of type captures associated with this parameter. Type captures define a type name within the attached code, which is an alias to the type gleaned from the argument during a call. The type used may change from call to call. Once they are defined, type captures can be used wherever you would use a type, even later in the same signature:
method sub_signature #
method sub_signature(Parameter:D: --> Signature:_)
Returns Signature:_.
If the parameter has a sub-signature, returns a Signature object for it. Otherwise returns Signature. Note: Before Rakudo release 2020.08 the return value for a parameter with no sub-signature was Any.
method prefix #
method prefix(Parameter:D: --> Str:D)
Returns Str:D.
If the parameter is slurpy, returns the marker (e.g., *, **, or +) the parameter was declared with. Otherwise, returns an empty string.
method suffix #
method suffix(Parameter:D: --> Str:D)
Returns Str:D.
Returns the ? or ! marker a parameter was declared with, if any. Otherwise, returns the empty string.
method modifier #
method modifier(Parameter:D: --> Str:D)
Returns Str:D.
Returns the type smiley (:U, :D, or :_) a parameter was declared with. It returns the empty string for the :_ type smiley. Parameter.new( ... ) In some situations, specifically when working with the MetaObject Protocol, it makes sense to create Parameter objects programmatically. For this purpose, you can call the new method with the following named parameters:
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
3 all-differ · 1 doc-drift · 2 no-output · 17 ok · 4 rakupp-differs
class Parameter { }Not executed: the documentation states no expected output for this example.
my $sig = :(Str $x); my $param = $sig.params[0]; say $param.type; # OUTPUT: «Str()»
Str()
(Str)
(Str)
Both engines agree; the documentation states something else. Trust the engines.
my Signature $sig = :(Str $x, Bool); say $sig.params[0].name; # OUTPUT: «$x» say $sig.params[1].name; # OUTPUT: «»
$x
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x, Str @*l, Bool); say $sig.params[0].usage-name; # OUTPUT: «x» say $sig.params[1].usage-name; # OUTPUT: «l» say $sig.params[2].usage-name; # OUTPUT: «»
x
l
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x, Bool :$is-named); say $sig.params[0].named; # OUTPUT: «False» say $sig.params[1].named; # OUTPUT: «True»
False
True
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x, Bool :$is-named); say $sig.params[0].positional; # OUTPUT: «True» say $sig.params[1].positional; # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
sub f($a, $b is raw, \c) {
my $sig = &?ROUTINE.signature;
for ^$sig.params.elems {
say $sig.params[$_].raw;
}
}
f(17, "4711", 42); # OUTPUT: «FalseTrueTrue»False
True
True
Documentation, Rakudo and Raku++ all agree.
sub f(\x) {
x = 5;
}
f(my $x); # works
f(42); # dies
CATCH { default { put .^name, ': ', .Str } };
# OUTPUT: «X::Assignment::RO: Cannot modify an immutable Int»X::Assignment::RO: Cannot modify an immutable Int
X::Assignment::RO: Cannot modify an immutable Int (42)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
sub f($x is raw) {
$x = 5;
}Not executed: the documentation states no expected output for this example.
my @types is List = Mu, Any;
say -> *@l { @l }(@types)[0] =:= @types[0]; # OUTPUT: «False»
say -> +@l { @l }(@types)[0] =:= @types[0]; # OUTPUT: «False»
say -> +l { l }(@types)[0] =:= @types[0]; # OUTPUT: «True»
say -> *@l is raw { @l }(@types)[0] =:= @types[0]; # OUTPUT: «True»False
False
True
True
False
False
True
True
True
True
True
True
Raku++ disagrees with both the documentation and Rakudo — a defect.
sub how_many_extra_positionals($!, |capture) { capture.elems.say }
how_many_extra_positionals(0, 1, 2, 3); # OUTPUT: «3»
say &how_many_extra_positionals.signature.params[1].capture; # OUTPUT: «True»3
True
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x is rw, Bool :$is-named); say $sig.params[0].rw; # OUTPUT: «True» say $sig.params[1].rw; # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x, Bool :$is-named is copy); say $sig.params[0].copy; # OUTPUT: «False» say $sig.params[1].copy; # OUTPUT: «True»
False
True
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str $x is rw, Bool :$is-named); say $sig.params[0].readonly; # OUTPUT: «False» say $sig.params[1].readonly; # OUTPUT: «True»
False
True
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :($i : Str $x is rw, Bool :$is-named); say $sig.params[0].invocant; # OUTPUT: «True» say $sig.params[1].invocant; # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :($a;; $b); say $sig.params[0].multi-invocant; # OUTPUT: «True» say $sig.params[1].multi-invocant; # OUTPUT: «False»
True
False
True
False
True
True
Raku++ disagrees with both the documentation and Rakudo — a defect.
my $sig = :($a, $b = 12); say $sig.params[0].default; # OUTPUT: «(Code)» say $sig.params[1].default.(); # OUTPUT: «12»
(Code)
12
Documentation, Rakudo and Raku++ all agree.
sub a(::T $x) { T.say }
a(8); # OUTPUT: «(Int)»
say &a.signature.params[0].type_captures; # OUTPUT: «(T)»
sub b($x) { $x.^name.say }
a(8); # OUTPUT: «Int»(Int)
(T)
Int
(Int)
(T)
(Int)
(T)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
sub c(::T $x, T $y, $z) { my T $zz = $z };
c(4, 5, 6); # OK
try c(4, 5, "six");
given $! { .message.say };
# OUTPUT: «Type check failed in assignment to $zz; expected Int but got Str ("six")»
try c("four", 5, "six");
given $! { .message.say };
# OUTPUT: «Type check failed in binding to parameter '$y'; expected Str but got Int (5)»Type check failed in assignment to $zz; expected Int but got Str ("six")
Type check failed in binding to parameter '$y'; expected Str but got Int (5)
Type check failed in assignment to $zz; expected Int but got Str ("six")
Type check failed in binding to parameter '$y'; expected Str but got Int (5)
Nil
Nil
Raku++ disagrees with both the documentation and Rakudo — a defect.
sub d(::T Numeric $x, T $y) {};
d(4, 5); # OK
d(4e0, 5e0); # OK
try d(4e0, 5);
given $! { .message.say };
# OUTPUT: «Type check failed in binding to parameter '$y'; expected Num but got Int (5)»
try d("four", "five");
given $! { .message.say };
# OUTPUT: «Type check failed in binding to parameter '$x'; expected Numeric but got Str ("four")»Type check failed in binding to parameter '$y'; expected Num but got Int (5)
Type check failed in binding to parameter '$x'; expected Numeric but got Str ("four")
Nil
Type check failed in binding to parameter '$x'; expected Numeric but got Str ("four")
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my Signature $sig = :(@array ($first, *@rest), @other); say $sig.params[0].sub_signature; # OUTPUT:«($first, *@rest)» say $sig.params[1].sub_signature; # OUTPUT:«(Signature)»
($first, *@rest)
(Signature)
($first, *@rest)
(Signature)
Raku++ disagrees with both the documentation and Rakudo — a defect.
my Signature $flat-slurpy = :($a, *@b); say $flat-slurpy.params[0].prefix; # OUTPUT:«» say $flat-slurpy.params[1].prefix; # OUTPUT:«*»
*
Documentation, Rakudo and Raku++ all agree.
my Signature $unflat-slurpy = :($a, **@b); say $unflat-slurpy.params[0].prefix; # OUTPUT:«» say $unflat-slurpy.params[1].prefix; # OUTPUT:«**»
**
Documentation, Rakudo and Raku++ all agree.
my Signature $sar-slurpy = :($a, +@b); say $sar-slurpy.params[0].prefix; # OUTPUT:«» say $sar-slurpy.params[1].prefix; # OUTPUT:«+»
+
Documentation, Rakudo and Raku++ all agree.
my Signature $pos-sig = :($a, $b?); say $pos-sig.params[0].suffix; # OUTPUT: «» say $pos-sig.params[1].suffix; # OUTPUT: «?»
?
Documentation, Rakudo and Raku++ all agree.
my Signature $named-sig = :(:$a!, :$b); say $named-sig.params[0].suffix; # OUTPUT: «!» say $named-sig.params[1].suffix; # OUTPUT: «»
!
Documentation, Rakudo and Raku++ all agree.
my Signature $sig = :(Str:U $a, UInt:D $b, $c); say $sig.params[0].modifier; # OUTPUT: «:U» say $sig.params[1].modifier; # OUTPUT: «:D» say $sig.params[2].modifier; # OUTPUT: «»
:U
:D
Documentation, Rakudo and Raku++ all agree.