Raku mixins 如何与运算符重载一起工作?

How can Raku mixins work with operator overloading?

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

unit module Physics::Error;

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

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

use Physics::Error;

my $x = 12.5 does Error(0.5);
my $z = -$x;

对'prefix:<->(Rat+{Physics::Error::Error})'的模糊调用;这些签名都匹配: (大鼠:D \a) (物理::错误::错误:D $右)

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

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

为你的复合体添加一个is default特征:

    multi prefix:<-> ( Error:D $right) is export is default {

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

is default is really a last resort, and even if you can get it to work using the mixin approach, you'll find the result is terribly slow, in no small part because mixins trigger deoptimization (falling out of specialized and JIT-compiled code back to the interpreter).