特质是否仅适用于对象? [英] Do will traits only apply to objects?

查看:44
本文介绍了特质是否仅适用于对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次在这个问题的尾部,我正在尝试做一个will特质工作,使用这个(打高尔夫球)代码:

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

这只是打印Going in".如果您在全局范围内执行此操作,则它(不会)以相同的方式工作.请注意,这几乎是文档示例的直接实现.

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.

推荐答案

您还没有注意到您的 Rakudo 版本.听起来像是今年引入的错误.

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

使用 glot.io 运行相同的代码:

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

这篇关于特质是否仅适用于对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆