This simplified (and somewhat contrived) example is pretty close to what I'm looking for.
implicit class PlusMinus(a: Double) {
def +-(b: Double) = if (a > b) a-b else a+b
}
With this I can:
scala> 3 +- 4L
res0: Double = 7.0
scala> 5f +- 1
res1: Double = 4.0
scala> 7L +- 6f
res3: Double = 1.0
But I have to wonder:
Every result is a Double. Can I imitate the automatic number conversions in the standard library?
Int +- Int = Int Long +- Long = Long Long +- Float = Float // etc.
Is there a better way? (There's always a better way.)
You could implement your operator like this
This quick solution has a problem, it'ok only with the same type in the two operands.
This one seems to respond to your problem :
We have that result
You can see in the last one, that the result type is the type of the second operand.