将角色混合成可调用角色

jjm*_*elo 5 mixins perl6

从理论上讲,您可以在运行时将角色混合到对象中.所以我试图用一个函数做到这一点:

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;
Run Code Online (Sandbox Code Playgroud)

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

Too few positionals passed; expected 1 argument but got 0
in block <unit> at self-call-me.p6 line 5
Run Code Online (Sandbox Code Playgroud)

我真的不知道谁会期待一个论点.从理论上讲,它应该是CALL-ME功能,但谁知道.消除self.产量的不同错误:CALL-ME used at line 11.添加does CallableArgable(把自己回来之后)会导致同样的错误.可以这样做吗?怎么想?

Eli*_*sen 6

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

say random-f.argh;  # *call* random-f and then call .argh on the result
Run Code Online (Sandbox Code Playgroud)

您要拨打.arghCallable这样:

say &random-f.argh;
Run Code Online (Sandbox Code Playgroud)

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

method argh(&self:) {
Run Code Online (Sandbox Code Playgroud)

所以最终的代码变成:

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;
Run Code Online (Sandbox Code Playgroud)