将角色混合到可调用对象中 [英] Mixing roles into callables

查看:62
本文介绍了将角色混合到可调用对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从理论上讲,您可以在运行时将角色混入对象中..因此,我正在尝试使用以下功能:

Theoretically, you can mix in a role into an object in runtime. So I am trying to do this with a function:

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 来实际调用角色内的函数.但是,这导致以下错误:

Within the role, I use self to refer to the already defined function, and CALL-ME to actually call the function within the role. However, this results in the following error:

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

我真的不知道谁期待1个论点.从理论上讲,它应该是CALL-ME函数,但是谁知道呢.消除self.会产生不同的错误:CALL-ME used at line 11.将does Callable添加到Argable(将自身放回原位)会导致相同的错误.能做到吗?有什么想法吗?

I really have no idea who's expecting 1 argument. Theoretically, it should be the CALL-ME function, but who knows. Eliminating the self. yields a different error: CALL-ME used at line 11. Adding does Callable to Argable (after putting self back) results in the same error. Can this be done? Any idea of how?

推荐答案

您的代码中有两点不正确:

There's two things incorrect in your code:

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

您要在Callable上呼叫.argh,因此:

say &random-f.argh;

第二,您应该只能调用self:您可以在.argh方法的签名中对其进行调整:

Secondly, you should just be able to call self: you can tweak this in the signature of the .argh method:

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;

这篇关于将角色混合到可调用对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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