I wrote some code in https://github.com/p6steve/raku-Physics-Measure that looks for a Measure type in each maths operation and hands off the work to non-standard methods that adjust Unit and Error aspects alongside returning the new value:
multi infix:<+> ( Measure:D $left, Real:D $right ) is export {
my $result = $left.clone;
my $argument = $right;
return $result.add-const( $argument );
}
multi infix:<+> ( Real:D $left, Measure:D $right ) is export {
my $result = $right.clone;
my $argument = $left;
return $result.add-const( $argument );
}
multi infix:<+> ( Measure:D $left, Measure:D $right ) is export {
my ( $result, $argument ) = infix-prep( $left, $right );
return $result.add( $argument );
}
This pattern is repeated 4 times for <[+-*/]> so it amounts to quite a lot of boilerplate; I'd like to reduce that a bit.
So, is there a more terse way to apply a single Measure|Real test in the signature to both Positionals in a way that the multi is triggered if both or one but not neither match and that the position is preserved for the intransigent operations <[-/]>?
I am not sure that getting to no multis is the most elegant - perhaps just compress the Real-Measure and Measure-Real to one?