I could use some help to work out if overloading math operators can be made to work with mixin via does
(or but
) in a way that avoids the ambiguity error below... this module:
unit module Physics::Error;
role Error is export {
has Real $.abs-error;
method negate {
...
}
}
multi prefix:<-> ( Error:D $right) is export {
...
}
used like by this script...
use Physics::Error;
my $x = 12.5 does Error(0.5);
my $z = -$x;
Ambiguous call to 'prefix:<->(Rat+{Physics::Error::Error})'; these signatures all match: (Rat:D \a) (Physics::Error::Error:D $right)
I want my custom operator to always win unambiguously, then for it to implement the core operation and the Error calcs and then return a (Rat+{Physics::Error::Error}).
Big picture is to do math operations that also perform simple error calculations.
Add an is default
trait to your multis:
multi prefix:<-> ( Error:D $right) is export is default {
That said, note jnthn's comment here:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With