Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are phaser traits run?

Tags:

traits

raku

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 enters 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?

like image 650
jjmerelo Avatar asked Jul 29 '21 12:07

jjmerelo


2 Answers

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).

like image 91
Elizabeth Mattijsen Avatar answered Jan 01 '23 20:01

Elizabeth Mattijsen


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.

like image 22
codesections Avatar answered Jan 01 '23 20:01

codesections