Function prototype
There is no restriction on placement of prototype declarations. (Actually, we call them "stub declarations".) In fact, stub declarations are rarely needed in Raku because post-declaration of functions is allowed, and normal function declarations do not bend the syntax the way they sometimes do in Perl 5.
Note that the ...
in all of these stub bodies is literally part of the declaration syntax.
A prototype declaration for a function that does not require arguments (and returns an Int):
A prototype declaration for a function that requires two arguments. Note that we can omit the variable name and just use the sigil in the stub, since we don't need to reference the argument until the actual definition of the routine. Also, unlike in Perl 5, a sigil like @
defaults to binding a single positional argument.
A prototype declaration for a function that utilizes varargs after one required argument. Note the "slurpy" star turns the @
sigil into a parameter that accepts all the rest of the positional arguments.
A prototype declaration for a function that utilizes optional arguments after one required argument. Optionality is conferred by either a question mark or a default:
A prototype declaration for a function that utilizes named parameters:
Example of prototype declarations for subroutines or procedures, which in Raku is done simply by noting that nothing is returned:
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
A routine may make a named parameter mandatory using exclamation mark. (This is more useful in multi subs than in stubs though.)
A routine may unpack an Array
automaticly. Here the first element is stored in a scalar and the rest in an Array
. Other buildin types can be unpacked as well.
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
Last updated