Rules / Types, classes & roles / basic

Code Reference

Code object

What it is #

Position in the hierarchy #

Inherits from
Does
Inherited byBlock, WhateverCode

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 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.

method arity #

method arity(Code:D: --> Int: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.

method assuming #

method assuming(Callable:D $self: |primers)

Returns a new Callable that has been primed with the arguments passed to assuming. In other words, the new function implements the same behavior as the original, but has the values passed to .assuming already bound to the corresponding parameters. For a sub with arity greater than one, you can use Whatever * for all of

method count #

method count(Code:D: --> Real: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.

method of #

method of(Code:D: --> Mu)

Returns Mu.

Returns the return type constraint of the Code:

method signature #

multi method signature(Code:D: --> Signature:D)

Returns Signature:D.

Returns the Signature object for this code object, which describes its parameters.

method cando #

method cando(Capture $c)

Returns a list of candidates that can be called with the given Capture. Since Code objects do not have any multiple dispatch, this either returns a list with the object, or an empty list.

method Str #

multi method Str(Code:D: --> Str:D)

Returns Str:D.

Will output the method name, but also produce a warning. Use .raku or .gist instead.

method file #

method file(Code:D: --> Str:D)

Returns Str:D.

Returns the name of the file in which the code object was declared.

method line #

method line(Code:D: --> Int:D)

Returns Int:D.

Returns the line number in the source code at which the code object's declaration begins. If the code object was generated automatically (and thus not declared in the source code), then line returns the line on which the enclosing scope's declaration begins. For example, when called on an automatically generated accessor method produced by the has $.name syntax, line returns the line on which the method's class's

method bytecode-size #

method bytecode-size(--> Int:D)

Returns Int:D.

Note: this method has been available in Rakudo compiler on the MoarVM backend only, starting from 2022.06 release. Returns the number of bytes that the code object occupies in memory. Note that if the code object is in fact a multi, then the bytecode size will be reported for the proto. You can use the .candidates method to obtain each candidate, and then call the bytecode-size methods on them.

method is-implementation-detail #

method is-implementation-detail(--> False)

Returns False.

Note: this method has been available in Rakudo compiler starting from 2020.05 release. Returns True if the code object was marked with is implementation-detail trait, False otherwise.

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.

4 all-differ · 6 no-output · 3 not-runnable · 8 ok · 1 rakupp-differs

class Code is Any does Callable {}

Not executed: the documentation states no expected output for this example.

sub argless() { }
sub args($a, $b?) { }
sub slurpy($a, $b, *@c) { }
say &argless.arity;             # OUTPUT: «0␤»
say &args.arity;                # OUTPUT: «1␤»
say &slurpy.arity;              # OUTPUT: «2␤»
Output
0
1
2

Documentation, Rakudo and Raku++ all agree.

my sub slow($n){ my $i = 0; $i++ while $i < $n; $i }

Not executed: the documentation states no expected output for this example.

# takes only one parameter and as such wont forward $n
sub bench(&c) { c, now - ENTER now }

Not executed: the documentation states no expected output for this example.

say &slow.assuming(10000000).&bench; # OUTPUT: «(10000000 7.5508834)␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.

sub first-and-last ( $first, $last ) {
    say "Name is $first $last";
}

Not executed: the documentation states no expected output for this example.

my &surname-smith = &first-and-last.assuming( *, 'Smith' );

Not executed: the documentation states no expected output for this example.

surname-smith( 'Joe' ); # OUTPUT: «Name is Joe Smith␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.

sub longer-names ( $first, $middle, $last, $suffix ) {
say "Name is $first $middle $last $suffix";
}

my &surname-public = &longer-names.assuming( *, *, 'Public', * );

surname-public( 'Joe', 'Q.', 'Jr.'); # OUTPUT: «Name is Joe Q. Public Jr.␤»
Output
Name is Joe Q. Public Jr.

Documentation, Rakudo and Raku++ all agree.

sub foo { say "$^a $^b $:foo $:bar" }
&foo.assuming(13, foo => 42)(24, bar => 72); # OUTPUT: «13 24 42 72␤»
Output
13 24 42 72

Documentation, Rakudo and Raku++ all agree.

# We use a Whatever star for the invocant:
my &comber = Str.^lookup('comb').assuming( *, /R \w+/ );
say comber 'Raku is awesome! Ruby is great! And Rust is OK too';
# OUTPUT: «(Raku Ruby Rust)␤»
Output
(Raku Ruby Rust)

Documentation, Rakudo and Raku++ all agree.

my &learner = {
    "It took me $:months months to learn $^lang"
}.assuming: 'Raku';
say learner months => 2;  # OUTPUT: «It took me 2 months to learn Raku␤»
Output
It took me 2 months to learn Raku

Documentation, Rakudo and Raku++ all agree.

sub argless() { }
sub args($a, $b?) { }
sub slurpy($a, $b, *@c) { }
say &argless.count;             # OUTPUT: «0␤»
say &args.count;                # OUTPUT: «2␤»
say &slurpy.count;              # OUTPUT: «Inf␤»
Output
0
2
Inf

Documentation, Rakudo and Raku++ all agree.

say -> () --> Int {}.of; # OUTPUT: «(Int)␤»
Output
(Int)

Documentation, Rakudo and Raku++ all agree.

sub a(Int $one, Str $two) {};
say &a.signature; # OUTPUT: «(Int $one, Str $two)␤»
Output
(Int $one, Str $two)

Documentation, Rakudo and Raku++ all agree.

my $single = \'a';         # a single argument Capture
my $plural = \('a', 42);   # a two argument Capture
my &block = { say $^a };   # a Block object, that is a subclass of Code, taking one argument
say &block.cando($single); # OUTPUT: «(-> $a { #`(Block|94212856419136) ... })␤»
say &block.cando($plural); # OUTPUT: «()␤»
Documentation
(-> $a { #`(Block|94212856419136) ... })
()
Rakudo
(-> $a { #`(Block|4890019324704) ... })
()
Raku++

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 marine() { }
say ~&marine;
# OUTPUT: «Sub object coerced to string (please use .gist or .raku to do that)␤marine␤»
say &marine.Str;
# OUTPUT: «Sub object coerced to string (please use .gist or .raku to do that)␤marine␤»
say &marine.raku; # OUTPUT: «sub marine { #`(Sub|94280758332168) ... }␤»
Documentation
Sub object coerced to string (please use .gist or .raku to do that)
marine
Sub object coerced to string (please use .gist or .raku to do that)
marine
sub marine { #`(Sub|94280758332168) ... }
Rakudo
marine
marine
sub marine { #`(Sub|2848387580312) ... }
Raku++

&marine
&marine

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.

say &infix:<+>.file;   # OUTPUT: «SETTING::src/core.c/Numeric.rakumod␤»
Documentation
SETTING::src/core.c/Numeric.rakumod
Rakudo
SETTING::src/core.c/Numeric.rakumod
Raku++

Raku++ disagrees with both the documentation and Rakudo — a defect.

say &infix:<+>.line;   # OUTPUT: «208␤»
Documentation
208
Rakudo
209
Raku++

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.

class Food {                # Line 1
    has $.ingredients;      # Line 2
                            # Line 3
    method eat {};          # Line 4
}                           # Line 5

Not executed: the documentation states no expected output for this example.

say Food.^lookup('eat').line;          # OUTPUT: «4␤»
say Food.^lookup('ingredients').line;  # OUTPUT: «1␤»
Documentation
4
1
Rakudo
1
1
Raku++

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.

say &grep.bytecode-size;              # OUTPUT: «114␤»
say &grep.cadidates>>.bytecode-size;  # OUTPUT: «424␤258␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.