Do will traits only apply to objects?

Viewed 75

Again on the tail of this question, I'm trying to make a will trait work, with this (golfed) code:

sub show-value( $a-var ) {
    say "Value of {$a-var.^name} is ", $a-var.gist;
}

sub do-stuff () {
    ENTER { say "Going in"; }
    our $bar will enter { show-value($_) };
    $bar = "baz";
    LEAVE { say "Leaving"; }
}

do-stuff();

This simply prints "Going in". It (doesn't) work(s) in the same way if you do it on the global scope. Please note that this is an almost direct implementation of the documentation example.

2 Answers

On 2021.07 I get:

Going in
Value of Any is (Any)
Leaving

A clearer example might be:

my $bar will enter { $_ = 42 }
say "bar = $bar";  # bar = 42
Related