Skip to content

Commit c685c58

Browse files
committed
Clarify topic variable's status in for loop
Issue: #3075
1 parent e7b73ea commit c685c58

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

doc/Language/control.pod6

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,19 @@ for $*IN.lines -> $line { .say }
562562
563563
Iteration variables are always lexical, so you don't need to use C<my> to give
564564
them the appropriate scope. Also, they are read-only aliases. If you need them
565-
to be read-write, use C«<->» instead of C«->». If you need to make C<$_>
566-
read-write in a for loop, do so explicitly.
565+
to be read-write, use C«<->» instead of C«->».
567566
568567
my @foo = 1..3;
569-
for @foo <-> $_ { $_++ }
568+
for @foo <-> $value { $value++ }
569+
570+
By default the topic variable C«$_» is a read-write alias. Thus, if you want
571+
to use it as a read-only alias you must indicate it by using C«->».
572+
573+
my @foo = 1..3;
574+
for @foo -> $_ { $_.say }
575+
576+
# Error: ...require mutable arguments
577+
for @foo -> $_ { $_++ }
570578
571579
A C<for> loop can produce a C<List> of the values produced by each run of the
572580
attached block. To capture these values, put the for loop in parenthesis or

0 commit comments

Comments
 (0)