代码之家  ›  专栏  ›  技术社区  ›  jjmerelo

特征是否仅适用于对象?

  •  2
  • jjmerelo  · 技术社区  · 3 年前

    又是在 this question ,我正试着做一个决定 will 特征工作,使用此(golfed)代码:

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

    这只是打印“进入”。如果在全局范围内执行,则它的工作方式不同。请注意,这几乎是 documentation example

    1 回复  |  直到 3 年前
        1
  •  4
  •   raiph    3 年前

    你还没有注意到你的Rakudo版本。这听起来像是今年引入的一个bug。

    Running the same code using glot.io :

    v2021.02.1
    Going in
    Value of Any is (Any)
    Leaving
    
        2
  •  0
  •   Elizabeth Mattijsen    3 年前

    Going in
    Value of Any is (Any)
    Leaving
    

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

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