Back to Intellij Community

AddOperatorModifier

plugins/kotlin/code-insight/descriptions/resources-en/inspectionDescriptions/AddOperatorModifier.html

2025.3-rc-2728 B
Original Source

Reports a function that matches one of the operator conventions but lacks the operator keyword.

By adding the operator modifier, you might allow function consumers to write idiomatic Kotlin code.

Example:

class Complex(val real: Double, val imaginary: Double) {
      fun plus(other: Complex) =
          Complex(real + other.real, imaginary + other.imaginary)
  }

  fun usage(a: Complex, b: Complex) {
      a.plus(b)
  }

The quick-fix adds the operator modifier keyword:

class Complex(val real: Double, val imaginary: Double) {
      operator fun plus(other: Complex) =
          Complex(real + other.real, imaginary + other.imaginary)
  }

  fun usage(a: Complex, b: Complex) {
      a + b
  }