Inheritance and Method Resolution
Call the next candidate with the same arguments, return to the current method
Call the next candidate with new arguments nextsame / nextwith(...)
Like the above, but don't return (tail call) Call the same dispatcher again (re-dispatch, e.g., multi recursion) Get the next candidate as a callable
Raku supports multiple inheritance (class C is A is B) using C3 MRO, but roles are preferred for code reuse.
class Animal {
has Str $.name is required;
method speak { "{$.name} makes a sound" }
method describe { "I am {$.name}, a {self.^name}" }
}
class Dog is Animal { # inheritance
has Str $.breed = 'mixed';
method speak { "{$.name} barks" } # override
method describe { callsame() ~ " ({$!breed})" } # call the parent's version
}
class Puppy is Dog {
method speak { nextsame } # defer to the next candidate (Dog.speak)
}
my @pets = Animal.new(name => 'Generic'), Dog.new(name => 'Rex', breed => 'Lab'), Puppy.new(name => 'Bit');
.speak.say for @pets;
say @pets[1].describe; # I am Rex, a Dog (Lab)
say Puppy.^mro; # ((Puppy) (Dog) (Animal) (Any) (Mu))
say Puppy ~~ Animal, ' ', Puppy.^parents;