10

I have a class

open class Texture

and I'd like to define the equals(other: Texture) operator

operator fun equals(other: Texture) = ...

but I get

Error:(129, 5) Kotlin: 'operator' modifier is inapplicable on this function: must override ''equals()'' in Any

What does it mean?

If I change that to

operator fun equals(other: Any) = ...

Accidental override, two declarations have the same jvm signature

elect
  • 6,765
  • 10
  • 53
  • 119

1 Answers1

8

The equals() operator function is defined in Any, so it should be overriden with a compatible signature: its parameter other should be of type Any?, and its return value should be Boolean or its subtype (it's final):

open class Texture {
    // ...

    override operator fun equals(other: Any?): Boolean { ... }
}

Without the override modifier, your function will clash with the Any::equals, hence the accidental override. Also, equals() cannot be an extension (just like toString()), and it cannot be overriden in an interface.

In IntelliJ IDEA, you can use Ctrl+O to override a member, or Ctrl+Insert to generate equals()+hashCode()

Community
  • 1
  • 1
hotkey
  • 140,743
  • 39
  • 371
  • 326