The will
phaser trait examples show this code:
our $h will enter { .rememberit() } will undo { .forgetit() };
Which is either misunderstood or simply not a real use case. If it's misunderstood, I would say it enter
s the block with it's assigned a variable. If it's not a real use case, it calls a method on an undefined variable. This is what seems to happen:
our &doing-good will enter {
say( "running" );
if Backtrace.new.grep: { .subname ~~ /bad/ } {
fail("Not authorized to call this");
}
};
This is simply run when the definition is made, that is, when what exactly is entered? The MAIN
scope?
This is probably the documentation fault. It's very likely that phaser traits can't really be applied to variables, but above, when it's actually a block, it's not really run; the "phaser" is run when something that's totally independent of the variable definition of value happens, at least in this case. Any idea?
The "phaser traits" as you call them, will be run at the same time as other phasers of the same name.
The will
trait on a variable basically sets up phasers in its surrounding scope, with the variable passed as the only positional parameter. So
my $h will enter { dd $_ };
is functionally equivalent to:
my $h;
ENTER { dd $h }
In your example:
our &doing-good will enter { ... }
you are defining a variable &doing-good
that will be passed to the block that is specified. In your example, I do not see that variable getting initialized, so the block will receive a Callable
type object (because that is what &doing-good
will contain if it is not initialized).
My understanding is that the will
trait is exactly the same as a the matching phaser – with the single difference that it has access to the variable as its topic. Thus the will enter
and the ENTER
below are very similar, and both execute when the containing scope is entered.
ENTER { note "ENTER phaser with topic $_.VAR.WHAT.raku()" }
my $a will enter { note "will enter with topic $_.VAR.WHAT.raku()"; $_ = 42 };
note 'main';
note "\$a: $a";
which prints:
ENTER phaser with topic Scalar
will enter with topic Scalar
main
$a: 42
Put differently, there's not a distinct "enter" phase that occurs for the variable; it's just referencing the ENTER
phase of the containing scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With