Skip to content

Commit fd7e010

Browse files
committed
Changes example of re-use of $ variable
Basically following the suggestions in the OP by @jimav. The second suggestion in the comment is probably a bit too verbose for something we wouldn't want people to do anyway. This closes #1864.
1 parent 4cc2a7f commit fd7e010

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

doc/Language/variables.pod6

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ State variables are shared between all threads. The result can be unexpected.
667667
# OUTPUT: «2␤1␤3␤4␤5␤»
668668
# many other more or less odd variations can be produced
669669
670-
=head3 The C<$> Variable
671670
X<|anon state variables>X<|nameless variables>X<|$ (variable)>
671+
=head3 The C<$> Variable
672672
673673
In addition to explicitly declared named state variables, C<$> can be used
674674
as an anonymous state variable without an explicit C<state> declaration.
@@ -691,38 +691,30 @@ perl6 -e '{ say ++$; say $++ } for ^5'
691691
# OUTPUT: «1␤0␤2␤1␤3␤2␤4␤3␤5␤4␤»
692692
=end code
693693
694-
If you need to use the value of $ more than once in a scope, it should be
695-
copied to a new variable.
694+
That is why, if you need to reference the same $ variable (or, for that matter, any of the other anon state variables C<@> and C<%>) more than once, a possible solution is to bind another variable to it, although in this example it would be more straightforward to just declare state $x and not use the magical/anonymous $ variable:
696695
697-
sub foo() {
698-
given ++$ {
699-
when 1 {
700-
say "one";
701-
}
702-
when 2 {
703-
say "two";
704-
}
705-
when 3 {
706-
say "three";
707-
}
708-
default {
709-
say "many";
710-
}
711-
}
712-
}
696+
=begin code
697+
sub foo () {
698+
my $x := $;
699+
$x++;
700+
say $x;
701+
$x = $x + 1;
702+
}
703+
704+
foo() for ^3; # OUTPUT: «1␤3␤5␤»
705+
=end code
713706
714-
foo() for ^3;
715-
# OUTPUT: «one␤two␤three␤»
707+
In general, it is better style and p6ier to declare a named state variable in case you have to refer to it several times.
716708
717709
Note that the implicit state declarator is only applied to the variable
718710
itself, not the expression that may contain an initializer. If the
719711
initializer has to be called exactly once, the C<state> declarator has to be
720712
provided.
721713
722-
=begin code :skip-test
723-
subset DynInt where $ = ::('Int'); # the initializer will be called for each type check
724-
subset DynInt where state $ = ::('Int'); # the initializer is called once, this is a proper cache
725-
=end code
714+
=begin code :skip-test
715+
subset DynInt where $ = ::('Int'); # the initializer will be called for each type check
716+
subset DynInt where state $ = ::('Int'); # the initializer is called once, this is a proper cache
717+
=end code
726718
727719
=head3 The C<@> Variable
728720

xt/words.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,3 +1382,4 @@ yyyy
13821382
zef
13831383
zoffixznet
13841384
zwj
1385+
ier

0 commit comments

Comments
 (0)