gpt4 book ai didi

mixins - 将角色混合到可调用对象中

转载 作者:行者123 更新时间:2023-12-04 18:20:09 27 4
gpt4 key购买 nike

理论上可以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

我真的不知道谁在期待 1 个论点。理论上应该是 CALL-ME功能,但谁知道。消除 self.产生不同的错误: CALL-ME used at line 11 .添加 does CallableArgable (将 self 放回去后)会导致相同的错误。这可以做到吗?知道怎么做吗?

最佳答案

您的代码中有两件事不正确:

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

您想调用 .arghCallable所以:
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;

关于mixins - 将角色混合到可调用对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50578740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com