Skip to content

Commit 78c92fc

Browse files
behaviour of once in classes
1 parent 19169cc commit 78c92fc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

doc/Language/traps.pod6

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,17 @@ In this example, we essentially manually build a C<once> block by making a C<sta
519519
520520
The main difference between using a C<state> variable like the above example and using a regular C<once> block is what scope the C<state> variable is in. The scope for the C<state> variable created by the C<once> block, is the same as where you put the block (imagine that the word 'C<once>' is replaced with a state variable and an C<if> to look at the variable). The example above using C<state> variables works because the variable is at the highest scope that will be repeated; whereas the example that has a C<once> block inside of a C<do>, made the variable within the C<do> block which is not the highest scope that is repeated.
521521
522+
Using a C<once> block inside a class method will cause the once state to carry across all instances of that class.
523+
For example:
524+
525+
class A {
526+
method sayit() { once say 'hi' }
527+
}
528+
my $a = A.new;
529+
$a.sayit; # says 'hi'
530+
my $b = A.new;
531+
$b.sayit; # nothing
532+
522533
=end pod
523534

524535
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)