加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

mixins – 将角色混合成可调用角色

发布时间:2020-12-16 06:26:54 所属栏目:大数据 来源:网络整理
导读:从理论上讲,你可以 mix in a role into an object in runtime.所以我试着用一个函数做到这一点: my random-f = - $arg { "Just $arg" };say random-f("boo");role Argable { method argh() { self.CALL-ME( "argh" ); }}random-f does Argable;say random-f
从理论上讲,你可以 mix in a role into an object in runtime.所以我试着用一个函数做到这一点:

my &random-f = -> $arg  { "Just $arg" };

say random-f("boo");

role Argable {
    method argh() {
        self.CALL-ME( "argh" );
    }
}

&random-f does Argable;

say random-f.argh;

在角色中,我使用self来引用已定义的函数,并使用CALL-ME来实际调用角色中的函数.但是,这会导致以下错误:

Too few positionals passed; expected 1 argument but got 0
in block <unit> at self-call-me.p6 line 5

我真的不知道谁会期待一个论点.从理论上讲,它应该是CALL-ME功能,但谁知道呢.消除自我.产生一个不同的错误:在第11行使用CALL-ME.添加Callable到Argable(放回自我后)导致相同的错误.可以这样做吗?怎么想?

解决方法

您的代码中有两个不正确的内容:

say random-f.argh;  # *call* random-f and then call .argh on the result

你想在Callable上调用.argh:

say &random-f.argh;

其次,你应该能够调用self:你可以在.argh方法的签名中调整它:

method argh(&self:) {

所以最终的代码变成:

my &random-f = -> $arg  { "Just $arg" };

say random-f("boo");

role Argable {
    method argh(&self:) {
        self( "argh" );
    }
}

&random-f does Argable;

say &random-f.argh;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读