perl6/doc/lib/Code
doc src
(title) Code
class Code is Any does Callable { ... }
Code is the ultimate base class of all code objects in Perl 6. It
exposes functionality that all code objects have. While thunks are
directly of type Code , most code objects (such as those resulting
from blocks, subroutines or methods) will be of some subclass of Code .
Methods
(head2) ACCEPTS
multi method ACCEPTS(Code:D: Mu $topic)
Usually calls the code object and passes $topic as an argument.
However, when called on a code object that takes no arguments, the code
object is invoked with no arguments and $topic is dropped. The
result of the call is returned.
(head2) arity
method arity(Code:D:) returns Int:D
Returns the minimum number of positional arguments that must be passed
in order to call the code object. Any optional or slurpy parameters in the
code object's Signature do not contribute, nor do named parameters.
sub argless() { }
sub args($a, $b?) { }
sub slurpy($a, $b, *@c) { }
say &argless.arity; # 0
say &args.arity; # 1
say &slurpy.arity; # 2
(head2) count
method count(Code:D:) returns Real:D
Returns the maximum number of positional arguments that may be passed
when calling the code object. For code objects that can accept any
number of positional arguments (that is, they have a slurpy parameter),
count will return Inf . Named parameters do not contribute.
sub argless() { }
sub args($a, $b?) { }
sub slurpy($a, $b, *@c) { }
say &argless.count; # 0
say &args.count; # 2
say &slurpy.count; # Inf
(head2) signature
multi method signature(Code:D:) returns Signature:D
Returns the Signature object for this code object, which describes
its parameters.
(head2) Str
multi method Str(Code:D:) returns Str:D
Returns the name of the code object.
sub marine() { }
say ~&marine; # marine