Skip to content

Commit ff73819

Browse files
committed
Improves introduction to Submethod with example refs #3063
1 parent 1a10d29 commit ff73819

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/Type/Submethod.pod6

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ C<submethod> declarator:
2424
}
2525
}
2626
27+
Since submethods are not inherited, an interesting use case is precisely
28+
methods that are going to be called from the I<standard> submethods such as
29+
C<BUILD> or C<TWEAK>.
30+
31+
=begin code
32+
class Hero {
33+
has @.inventory;
34+
has Str $.name;
35+
submethod BUILD( :$!name, :@!inventory ) {
36+
@!inventory = self.clean-inventory( @!inventory );
37+
}
38+
submethod clean-inventory( @inventory ) {
39+
@!inventory.unique.sort
40+
}
41+
}
42+
43+
my Hero $þor .= new( name => "Þor",
44+
inventory => ( "Mjollnir", "Megingjörð", "Mjollnir" ) );
45+
say $þor.inventory;
46+
# OUTPUT: «[Megingjörð Mjollnir]␤»
47+
=end code
48+
49+
Invoking these methods make sense only in the specific context of the
50+
submethod it is invoked from.
51+
2752
=head1 Methods
2853
2954
=head2 method gist

0 commit comments

Comments
 (0)