Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clarifies scope of state closes #2616
  • Loading branch information
JJ committed Feb 13, 2019
1 parent 4855a73 commit d0b656b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions doc/Language/variables.pod6
Expand Up @@ -641,24 +641,40 @@ Therefore, the subroutine
will continue to increment C<$l> and append it to C<@x> each time it is
called. So it will output:
=begin code :skip-test
=for code :lang<text>
[A]
[A B]
[A B C]
[A B C D]
[A B C D E]
[A B C D E F]
=end code
Since they have a lexical scope, they are tied to the block they are
declared.
=for code
sub foo () {
for 0..1 {
state $foo = 1;
say $foo++;
}
};
foo; # OUTPUT: «1␤2␤»
foo; # OUTPUT: «1␤2␤»
This works per "clone" of the containing code object, as in this example:
In this case, a new state variable is created every time the block that
runs the for loop is entered, which is why the state variable is reset
in every call to C<foo>.
({ state $i = 1; $i++.say; } xx 3).map: {$_(), $_()}; # says 1 then 2 thrice
This works per "clone" of the containing code object, as in this
example:
=for code
({ state $i = 1; $i++.say; } xx 3).map: {$_(), $_()}; # says 1 then 2 thrice
Note that this is B<not> a thread-safe construct when the same clone of the same
block is run by multiple threads. Also remember that methods only have one
clone per class, not per object.
Note that this is B<not> a thread-safe construct when the same clone of
the same block is run by multiple threads. Also remember that methods
only have one clone per class, not per object.
As with C<my>, a declaration of multiple C<state> variables must be placed
in parentheses which can be omitted for a single variable.
Expand Down

0 comments on commit d0b656b

Please sign in to comment.