Equals, toString and extension methods


#1

From the documentation, I understand that it is not necessary to use override when supplying equals and toString methods for classes, defining the method is enough, i.e.:

class Thing(argId : Int) {

  val id = argId

  func equals(other : Any?) : Boolean {

          return this.id == other.id

  }

}

I also discovered that these methods cannot be supplied as extension methods:

// outside the class…  is never called
  func Thing.equals(other : Any?) : Boolean {
          return this.id == other.id
  }

I’m curious, is this the intended behavior?


#2

The first part: override is not required (and is actually prohibited) for toString() etc in some classes.

This will be fixed.

The second part: defining an extension equals() does not make, say, HashMap notice it

This is inevitable: extensions are bound statically, so generic code can’t rely on them, also, a member always wins over an extesion in overload resolution.
We will add diagnostics to warn the user when s/he defines an extension that will never be caled, like equals(Any?)