๐Ÿ“˜ Method postfix operator .+ in Perl 6

๐Ÿ“˜ Method postfix operator .+ in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

.+method makes an attempt to call all the methods with the given name on an object. This may be used, for example, when an instance is a part of the hierarchy of objects and its parent also has a method with the same name. More on the classes and class hierarchy in Chapter 4.

class A {
ย ย ย  method m($x) {"A::m($x)"}
class B is A {
ย ย ย  method m($x) {"B::m($x)"}

my $o = B.new;
my @a = $o.+m(7);
say @a; # Prints [B::m(7) A::m(7)]

Here, the $o object has the m method in both its own class B and in its parent class A. The $o.+m(7) calls both of the methods and puts their results in a list.

If the method is not defined, an exception will be thrown.

Leave a Reply

Your email address will not be published. Required fields are marked *