理论上,你可以在运行时将角色混合到对象中 https://docs.raku.org/language/objects#Mixins_of_Roles。所以我尝试用一个函数来做到这一点:
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 https://docs.perl6.org/routine/CALL-ME实际调用角色内的函数。但是,这会导致以下错误:
Too few positionals passed; expected 1 argument but got 0
in block <unit> at self-call-me.p6 line 5
我真的不知道谁在期待一场争论。理论上应该是CALL-ME
功能,但谁知道呢。消除self.
产生不同的错误:CALL-ME used at line 11
。添加does Callable
to Argable
(将自身放回去后)会导致相同的错误。这可以做到吗?知道怎么做吗?
您的代码中有两处不正确:
say random-f.argh; # *call* random-f and then call .argh on the result
你想打电话.argh
on the Callable
so:
say &random-f.argh;
其次,你应该能够调用self
:你可以在签名中调整这个.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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)