class Method

Member function

class Method is Routine { }

A type for methods that behave the same way then Routine with some exceptions listed in the following. For details of a methods parameter list see Signature.

To create a method outside a class definition, use the declarators my and method. If an identifier is provided the methods name will be injected into the scope specified by the declarator.

my $m = method ($invocant: $param{
    say "$invocant: '$param'";
}
"greeting".$m("hello");  # OUTPUT: «greeting: 'hello'␤» 
 
<a b c>.&(my method (List:D:{ say self.perlself }).say;
# OUTPUT: «("a", "b", "c")␤(a b c)␤» 

The invocant of a method defaults to self. A type constraint including a type-smiley can be used and is honored both for methods defined in a class and for free floating methods. Call the latter with .& on an object.

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

Methods will ignore extra named arguments where other types of Routine will throw at runtime. Extra arguments will be forwarded by nextsame and friends.

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

sub lastcall

sub lastcall(--> True)

Truncates the current dispatch chain, which means any calls to nextsame, callsame, nextwith, and callwith will not find any of the next candidates. Note that since samewith restarts the dispatch from the start, it's not affected by the truncation of current chain with lastcall.

Consider example below. foo(6) uses nextsame when lastcall hasn't been called, and so it reaches the Any candidate. foo(2) calls nextsame as well, but since lastcall was called first, the dispatch chain was truncated and the Any candidate was not reached. The last call, foo(1), calls lastcall too, however, it then uses samewith, which isn't affected by it, and so the dispatch re-starts from scratch, hits the Int candidate with the new argument 6, and then proceeds to the Any candidate via nextsame (which isn't affected by the lastcall that was used before the samewith was called):

multi foo (Int $_{
    say "Int: $_";
    lastcall   when *.is-prime;
    nextsame   when *  %% 2;
    samewith 6 when * !%% 2;
}
multi foo (Any $x{ say "Any $x" }
 
foo 6say '----';
foo 2say '----';
foo 1;
 
# OUTPUT: 
# Int: 6 
# Any 6 
# ---- 
# Int: 2 
# ---- 
# Int: 1 
# Int: 6 
# Any 6 

Type Graph

Type relations for Method
perl6-type-graph Method Method Routine Routine Method->Routine Mu Mu Any Any Any->Mu Callable Callable Code Code Code->Any Code->Callable Block Block Block->Code Routine->Block Regex Regex Regex->Method

Stand-alone image: vector

Routines supplied by class Routine

Method inherits from class Routine, which provides the following routines:

(Routine) method name

method name(Routine:D: --> Str:D)

Returns the name of the sub or method.

(Routine) method package

method package(Routine:D:)

Returns the package in which the routine is defined.

(Routine) method multi

method multi(Routine:D: --> Bool:D)

Returns True if the routine is a multi sub or method. Note that the name of a multi sub refers to its proto and this method would return false if called on it. It needs to be called on the candidates themselves:

multi foo ($, $) {};
say &foo.multi;             # OUTPUT: «False␤» 
say &foo.candidates».multi# OUTPUT: «(True)␤» 

(Routine) method candidates

method candidates(Routine:D: --> Positional:D)

Returns a list of multi candidates, or a one-element list with itself if it's not a multi

(Routine) method cando

method cando(Capture $c)

Returns a possibly-empty list of candidates that can be called with the given Capture, ordered by narrowest candidate first. For methods, the first element of the Capture needs to be the invocant:

.signature.say for "foo".^can("comb")[0].cando: \(Cool"o");
# OUTPUT: «(Cool $: Str $matcher, $limit = Inf, *%_)␤» 

(Routine) method wrap

method wrap(Routine:D: &wrapper)

Wraps (i.e. in-place modifies) the routine. That means a call to this routine first calls &wrapper, which then can (but doesn't have to) call the original routine with the callsame, callwith, nextsame and nextwith dispatchers. The return value from the routine is also the return value from the wrapper.

wrap returns an instance of a private class called WrapHandle, which you can pass to unwrap to restore the original routine.

(Routine) method unwrap

method unwrap($wraphandle)

Restores the original routine after it has been wrapped with wrap. While the signature allows any type to be passed, only the WrapHandle type returned from wrap can usefully be.

(Routine) method yada

method yada(Routine:D: --> Bool:D)

Returns True if the routine is a stub

say (sub f() { ... }).yada;      # OUTPUT: «True␤» 
say (sub g() { 1;  }).yada;      # OUTPUT: «False␤» 

(Routine) trait is cached

multi sub trait_mod:<is>(Routine $r:$cached!)

NOTE: this feature is currently EXPERIMENTAL.

Causes the return value of a routine to be stored, so that when subsequent calls with the same list of arguments are made, the stored value can be returned immediately instead of re-running the routine.

Useful when storing and returning the computed value is much faster than re-computing it every time, and when the time saving trumps the cost of the use of more memory.

Even if the arguments passed to the routine are "reference types" (such as objects or arrays), then for the purpose of caching they will only be compared based on their contents. Thus the second invocation will hit the cache in this case:

say foo( [123] );   # runs foo 
say foo( [123] );   # doesn't run foo, uses cached value 
use experimental :cached;
 
sub nth-prime(Int:D $x where * > 0is cached {
    say "Calculating {$x}th prime";
    return (2..*).grep(*.is-prime)[$x - 1];
}
 
say nth-prime(43);
say nth-prime(43);
say nth-prime(43);

produces this output:

Calculating 43th prime
191
191
191

(Routine) trait is pure

multi sub trait_mod:<is>(Routine $r:$pure!)

Marks a subroutine as pure, that is, it asserts that for the same input, it will always produce the same output without any additional side effects.

You can mark function as pure even if they throw exceptions in edge cases, or if they modify temporary objects; hence the is pure trait can cover cases that the compiler cannot deduce on its own. On the other hand, you might not want to constant-fold functions that produce a large return value (such as the string or list repetition operators, infix x and xx) even if they are pure, to avoid large precompilation files.

The is pure trait is a promise by the programmer to the compiler that it can constant-fold calls to such functions when the arguments are known at compile time.

sub syllables() is pure {
    say "Generating syllables";
    my @vowels = <a e i o u>;
    return  @vowels.append: <k m n sh d r t y> X~ @vowels;
}

To see it an action with a particular compiler you can try this example:

BEGIN { say Begin }
say Start;
say (^100).map: { syllables().pick(2..5).join(""};
 
 
# Example output: 
# Begin 
# Generating syllables 
# Start 
# (matiroi yeterani shoriyuru... 

Essentially this allows the compiler to perform some operations at compile time. The benefits of constant-folding may include better performance, especially in cases when the folded code is precompiled.

In addition, using a pure function or operator in sink context (that is, where the result is discarded) may lead to a warning. The code

sub double($xis pure { 2 * $x };
double(21);
say "anything";
# WARNING: «Useless use of "double(21)" in expression "double(21)" in sink context (line 2)» 

(Routine) trait is rw

multi sub trait_mod:<is>(Routine $r:$rw!)

When a routine is modified with this trait, its return value will be writable. This is useful when returning variables or writable elements of hashes or arrays, for example:

sub walk(\thing*@keysis rw {
    my $current := thing;
    for @keys -> $k {
        if $k ~~ Int {
            $current := $current[$k];
        }
        else {
            $current := $current{$k};
        }
    }
    $current;
}
 
my %hash;
walk(%hash'some''key'12= 'autovivified';
 
say %hash.perl;

produces

("some" => {"key" => [Any, [AnyAny"autovivified"]]}).hash

Note that return marks return values as read only; if you need an early exit from an is rw routine, you have to use return-rw instead.

(Routine) trait is export

multi sub trait_mod:<is>(Routine $r:$export!)

Marks a routine as exported to the rest of the world

module Foo {
    sub double($x) is export {
        2 * $x
    }
}

import Foo;         # makes sub double available
say double 21;      # 42

From inside another file you'd say use Foo; to load a module and import the exported functions.

See Exporting and Selective Importing Modules for more details.

(Routine) trait is DEPRECATED

multi sub trait_mod:<is>(Routine:D $r:$DEPRECATED!)

Marks a routine as deprecated, optionally with a message what to use instead.

This code

sub f() is DEPRECATED('the literal 42'{ 42 }
say f();

produces this output:

42
Saw 1 occurrence of deprecated code.
================================================================================
Sub f (from GLOBALseen at:
  deprecated.p6line 2
Please use the literal 42 instead.
--------------------------------------------------------------------------------
Please contact the author to have these occurrences of deprecated code
adaptedso that this message will disappear!

(Routine) trait is hidden-from-backtrace

multi sub trait_mod:<is>(Routine:D:$hidden-from-backtrace!)

Hides a routine from showing up in a default backtrace. For example

sub inner { die "OH NOEZ" };
sub outer { inner() };
outer();

produces the error message and backtrace

OH NOEZ
  in sub inner at bt.p6:1
  in sub outer at bt.p6:2
  in block <unit> at bt.p6:3

but if inner is marked with hidden-from-backtrace

sub inner is hidden-from-backtrace { die "OH NOEZ" };
sub outer { inner() };
outer();

the error backtrace does not show it:

OH NOEZ
  in sub outer at bt.p6:2
  in block <unit> at bt.p6:3

Routines supplied by class Code

Method inherits from class Code, which provides the following routines:

(Code) 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.

(Code) method arity

Defined as:

method arity(Code:D: --> 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;             # OUTPUT: «0␤» 
say &args.arity;                # OUTPUT: «1␤» 
say &slurpy.arity;              # OUTPUT: «2␤» 

(Code) method assuming

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

Returns a Callable that implements the same behaviour as the original, but has the values passed to .assuming already bound to the corresponding parameters.

my sub slow($n){ my $i = 0$i++ while $i < $n$i };
 
# takes only one parameter and as such wont forward $n 
sub bench(&c){ cnow - ENTER now };
 
say &slow.assuming(10000000).&bench# OUTPUT: «(10000000 7.5508834)␤» 

For a sub with arity greater than one, you can use Whatever * for all of the positional parameters that are not "assumed".

sub first-and-last ( $first$last ) {
    say "Name is $first $last";
}
 
my &surname-smith = &first-and-last.assuming*'Smith' );
 
&surname-smith.'Joe' ); # OUTPUT: «Name is Joe Smith␤» 

You can handle any combination of assumed and not assumed positional parameters:

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

Named parameters can be assumed as well:

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

And you can use .assuming on all types of Callables, including Methods and Blocks:

# We use a Whatever star for the invocant: 
my &comber = Str.^lookup('comb').assuming: *, /\w+/;
say comber 'Perl is awesome! Python is great! And PHP is OK too';
# OUTPUT: «(Perl Python PHP)␤» 
 
my &learner = {
    "It took me $:months months to learn $^lang"
}.assuming: 'Perl 6';
say learner :6months;  # OUTPUT: «It took me 6 months to learn Perl 6␤» 

(Code) method count

Defined as:

method count(Code:D: --> 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;             # OUTPUT: «0␤» 
say &args.count;                # OUTPUT: «2␤» 
say &slurpy.count;              # OUTPUT: «Inf␤» 

(Code) method signature

Defined as:

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

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

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

(Code) method Str

Defined as:

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

Will produce a warning. Use .perl or .gist instead.

sub marine() { }
say ~&marine;    # OUTPUT: «marine␤» 
say &marine.Str# OUTPUT: «marine␤» 

(Code) method file

Defined as:

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

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

say &infix:<+>.file;

(Code) method line

Defined as

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

Returns the line number in which the code object was declared.

say &infix:<+>.line;

Routines supplied by role Callable

Method inherits from class Code, which does role Callable, which provides the following routines:

(Callable) method CALL-ME

method CALL-ME(Callable:D $self: |arguments)

This method is required for postfix:«( )» and postfix:«.( )». It's what makes an object actually call-able and needs to be overloaded to let a given object act like a routine. If the object needs to be stored in a &-sigiled container, is has to implement Callable.

class A does Callable {
    submethod CALL-ME(|c){ 'called' }
}
my &a = A;
say a(); # OUTPUT: «called␤» 

(Callable) method Capture

Defined as:

method Capture()

Throws X::Cannot::Capture.