Raku mixin 如何与运算符重载配合使用?

p6s*_*eve 10 units-of-measurement raku

does我可以使用一些帮助来确定是否可以通过(或)使重载数学运算符与 mixin 一起使用but,从而避免下面的歧义错误...此模块:

unit module Physics::Error;

role Error is export {
        has Real $.abs-error;
    
        method negate {
            ...
        }
    }
    
    multi prefix:<-> ( Error:D $right) is export {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

像这个脚本一样使用...

use Physics::Error;

my $x = 12.5 does Error(0.5);
my $z = -$x;
Run Code Online (Sandbox Code Playgroud)

对 'prefix:<->(Rat+{Physics::Error::Error})' 的调用不明确;这些签名全部匹配: (Rat:D \a) (Physics::Error::Error:D $right)

我希望我的自定义运算符始终明确获胜,然后让它实现核心操作和错误计算,然后返回 (Rat+{Physics::Error::Error})。

大局是进行数学运算,同时也执行简单的误差计算。

rai*_*iph 5

为你的多重属性添加一个is default特质:

    multi prefix:<-> ( Error:D $right) is export is default {
Run Code Online (Sandbox Code Playgroud)

也就是说,请注意 jnthn 的评论

is default确实是最后的手段,即使你可以使用 mixin 方法让它工作,你也会发现结果非常慢,很大程度上是因为 mixin 触发了去优化(从专门的和 JIT 编译的代码中掉出来)口译员)。