Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do will traits only apply to objects?

Tags:

traits

raku

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.

like image 482
jjmerelo Avatar asked Mar 01 '23 12:03

jjmerelo


2 Answers

You haven't noted your Rakudo version. It sounds like a bug introduced this year.

Running the same code using glot.io:

v2021.02.1
Going in
Value of Any is (Any)
Leaving
like image 166
raiph Avatar answered Mar 12 '23 19:03

raiph


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
like image 36
Elizabeth Mattijsen Avatar answered Mar 12 '23 20:03

Elizabeth Mattijsen