You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: doc/Language/variables.pod6
+17-25Lines changed: 17 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -667,8 +667,8 @@ State variables are shared between all threads. The result can be unexpected.
667
667
# OUTPUT: «21345»
668
668
# many other more or less odd variations can be produced
669
669
670
-
=head3The C<$> Variable
671
670
X<|anon state variables>X<|nameless variables>X<|$ (variable)>
671
+
=head3The C<$> Variable
672
672
673
673
In addition to explicitly declared named state variables, C<$> can be used
674
674
as an anonymous state variable without an explicit C<state> declaration.
@@ -691,38 +691,30 @@ perl6 -e '{ say ++$; say $++ } for ^5'
691
691
# OUTPUT: «1021324354»
692
692
=endcode
693
693
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:
696
695
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
+
=begincode
697
+
sub foo () {
698
+
my $x := $;
699
+
$x++;
700
+
say $x;
701
+
$x = $x + 1;
702
+
}
703
+
704
+
foo() for ^3; # OUTPUT: «135»
705
+
=endcode
713
706
714
-
foo() for ^3;
715
-
# OUTPUT: «onetwothree»
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.
716
708
717
709
Note that the implicit state declarator is only applied to the variable
718
710
itself, not the expression that may contain an initializer. If the
719
711
initializer has to be called exactly once, the C<state> declarator has to be
720
712
provided.
721
713
722
-
=begincode :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
-
=endcode
714
+
=begincode :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
0 commit comments