Skip to content

Commit 41a7f6e

Browse files
committed
doc trap of ~~ in conjunction with .method-name
1 parent 05c700e commit 41a7f6e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/Language/traps.pod6

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ starting string:
279279
say join ", ", ("az", *.succ ... "bc")
280280
az, ba, bb, bc
281281
282+
The smart match operator C<~~> does set the topic C<$_> to its LHS. In
283+
conjunction with implicit method call on the topic this can lead to surprising
284+
results.
285+
286+
my &method = { note $_; $_ };
287+
$_ = 'object';
288+
say .&method;
289+
# OUTPUT«object␤object␤»
290+
say 'topic' ~~ .&method;
291+
# OUTPUT«topic␤True␤»
292+
293+
In many cases flipping the method call to the LHS will work.
294+
295+
my &method = { note $_; $_ };
296+
$_ = 'object';
297+
say .&method;
298+
# OUTPUT«object␤object␤»
299+
say .&method ~~ 'topic';
300+
# OUTPUT«object␤True␤»
301+
282302
=head1 Common Precedence Mistakes
283303
284304
=head2 Adverbs and Precedence

0 commit comments

Comments
 (0)