Skip to content

Commit d1f2c6a

Browse files
authored
Correct and expand &lastcall docs
1 parent 0ac09ee commit d1f2c6a

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

doc/Type/Method.pod6

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,45 @@ L<nextsame|/language/functions#index-entry-nextsame-nextsame> and friends.
5353
5454
sub lastcall(--> True)
5555
56-
To stop dispatch in a multi method chain of the current class call C<lastcall>.
57-
Any call for L<nextsame|/language/functions#index-entry-nextsame-nextsame> and
58-
friends will continue in the next dispatcher loop (typically a parent class)
59-
after C<lastcall> is called.
56+
Truncates the current dispatch chain, which means any calls to
57+
C<nextsame>, C<callsame>, C<nextwith>, and C<callwith> will not
58+
find any of the next candidates. Note that since C<samewith>
59+
restarts the dispatch from the start, it's not affected by the
60+
truncation of current chain with C<lastcall>.
61+
62+
Consider example below. C<foo(6)> uses C<nextsame>
63+
when C<lastcall> hasn't been called, and so it reaches the C<Any>
64+
candidate. C<foo(2)> calls C<nextsame> as well, but since
65+
C<lastcall> was called first, the dispatch chain was truncated and
66+
the C<Any> candidate was not reached. The last call, C<foo(1)>,
67+
calls C<lastcall> too, however, it then uses C<samewith>, which
68+
isn't affected by it, and so the dispatch re-starts from scratch,
69+
hits the C<Int> candidate with the new argument C<6>, and then
70+
proceeds to the C<Any> candidate via C<nextsame> (which
71+
isn't affected by the C<lastcall> that was used before the
72+
C<samewith> was called):
73+
74+
multi foo (Int $_) {
75+
say "Int: $_";
76+
lastcall when *.is-prime;
77+
nextsame when * %% 2;
78+
samewith 6 when * !%% 2;
79+
}
80+
multi foo (Any $x) { say "Any $x" }
81+
82+
foo 6; say '----';
83+
foo 2; say '----';
84+
foo 1;
85+
86+
# OUTPUT:
87+
# Int: 6
88+
# Any 6
89+
# ----
90+
# Int: 2
91+
# ----
92+
# Int: 1
93+
# Int: 6
94+
# Any 6
95+
6096
6197
=end pod

0 commit comments

Comments
 (0)