I am trying to figure out how to override equality. More specifically I am implementing dual numbers to be used in forward mode automatic differentiation. To test this code I need to be able to compare duals within an eps.
I am using the latest dotty RC and have looked at multiversal equality. But I assume that to override equality as a I want, typeclass derivation is not what I need.
The multiversal equality feature of Dotty, as far as I understand it, is just used to let the compiler check if a comparison between two types is legal. To override the equality, you still have to override the equals method, like with Scala 2 or Java.
If you did that, but you enabled strictEquality or defined an Eql[Dual, Dual], then dotty doesn’t allow comparison because an eps is something of a different type, you should add an Eql instance, so the compiler knows this is legal. The following would declare, that a Dual can be compared to another Dual or a Eps:
given Eql[Dual, Dual] = Eql.derived
given Eql[Dual, Eps] = Eql.derived
The method doing the comparison is still the equals method, so you’ll have to handle both cases there.