class Submethod is Routine {}

A Submethod is a method that is not inherited by child classes. They are typically used for per-class initialization and tear-down tasks which are called explicitly per class in an inheritance tree, usually for enforcing a particular order. For example object construction with the BUILD submethod happens from the least-derived to most-derived, so that the most-derived (child) classes can depend on the parent already being initialized.

Submethods are of type Submethod, and are declared with the submethod declarator:

class Area {
    has $.size;
    submethod BUILD(:$x, :$y, :$z) {
        $!size = $x * $y * $z;
    }
}

Since submethods are not inherited, an interesting use case is precisely methods that are going to be called from the standard submethods such as BUILD or TWEAK.

class Hero {
    has @.inventory;
    has Str $.name;
    submethod BUILD( :$!name, :@!inventory ) {
        @!inventory = self.clean-inventory( @!inventory );
    }
    submethod clean-inventory( @inventory ) {
        @!inventory.unique.sort
    }
}

my Hero $þor .= new( name => "Þor",
                     inventory => ( "Mjölnir", "Megingjörð", "Mjölnir" ) );
say $þor.inventory;
# OUTPUT: «[Megingjörð Mjölnir]␤»

Invoking these methods make sense only in the specific context of the submethod it is invoked from.

Methods§

method gist§

multi method gist(Submethod:D:)

Returns the name of the submethod.

Typegraph§

Type relations for Submethod
raku-type-graph Submethod Submethod Routine Routine Submethod->Routine Mu Mu Any Any Any->Mu Callable Callable Code Code Code->Any Code->Callable Block Block Block->Code Routine->Block

Expand chart above