Dotty: how to override equality correctly

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.

Any pointers?

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.

@crater2150 Ok, this sets stricter compile time checking. I have enabled strict equality, but I still need to set run-time behavior. Silly me.

Thank you.

Side note - the equality check I want is to allow for something of the kind (very simplified):

case class Dual1D(r:Double, d:Double=1.0) {

  val EPS = 1e-12

  override def equals(o: Any): Boolean = {
    o match {
      case o:Dual1D =>
        (Math.abs(r-o.r) < EPS) &&
        (Math.abs(d-o.d) < EPS)
      case _ => false