特质是否仅适用于对象?

编程入门 行业动态 更新时间:2024-10-28 10:21:23
本文介绍了特质是否仅适用于对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

再次在这个问题的尾部,我正在尝试做一个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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 01:54:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   特质   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!