Rules / Types, classes & roles / basic

Method Reference

Member function

What it is #

Position in the hierarchy #

Inherits from
Does
Inherited byRegex

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 no-output · 1 not-runnable · 3 ok

class Method is Routine { }

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

my $m = method ($invocant: $param) {
    say "$invocant: '$param'";
}
"greeting".$m("hello");  # OUTPUT: «greeting: 'hello'␤»
Output
greeting: 'hello'

Documentation, Rakudo and Raku++ all agree.

<a b c>.&(my method (List:D:) { say self.raku; self }).say;
# OUTPUT: «("a", "b", "c")␤(a b c)␤»
Output
("a", "b", "c")
(a b c)

Documentation, Rakudo and Raku++ all agree.

my method m(Int:D: $b){
    say self.^name
}
my $i = 1;
$i.&m(<a>);
# OUTPUT: «Int␤»
Output
Int

Documentation, Rakudo and Raku++ all agree.

method x() {}

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

method x(*%_) {}

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

class A {
    multi method m(:$a, :$b) { say "2 named" }
}

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

class B is A {
    method m(:$a) { say "1 named"; nextsame }
}
B.m( :1a, :2b );
# OUTPUT: «1 named␤2 named␤»

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