特征是否仅适用于 objects?

Do will traits only apply to objects?

再次在 this question 的尾巴上,我正在尝试使 will 特征起作用,使用此(高尔夫)代码:

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();

这只是打印“Going in”。如果您在全球范围内进行,它(不会)以相同的方式工作。请注意,这几乎是 documentation example.

的直接实现

您还没有记下您的 Rakudo 版本。听起来像是今年引入的错误。

Running the same code using glot.io:

v2021.02.1
Going in
Value of Any is (Any)
Leaving

2021.07 我得到:

Going in
Value of Any is (Any)
Leaving

一个更清晰的例子可能是:

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