Why must certain operators return a value in EK9?

← Classes and OOP · Ref: Q779

In EK9, computational operators (like <, ==, +, -, <>) must return a value. These operators are expressions — they compute something. Without a return declaration, the operator has no result and cannot be used in expressions.

OPERATORS THAT MUST RETURN

- Comparison: < must return Boolean
- Equality: == must return Boolean
- Arithmetic: +, - must return the same type
- Spaceship: <=> must return Integer
- Hash: #? must return Integer
- String: $ must return String

MUTATING OPERATORS (no return needed)
- +=, -=, :=:, :~:, :^: modify in place, return Void
- ++, -- increment/decrement in place
- close releases resources

THIS EXAMPLE

The Temperature class defines < and == operators with correct return declarations. The mutation removes the return from the < operator.

See Q81 for operator overview. See Q239 for comparison operators. See Q82 for default operators.

Example

defines module qa.classesandoop.operatorreturn

  defines class

    Temperature
      degrees <- Float()

      Temperature()
        -> d as Float
        degrees :=: d

      operator < as pure
        -> other as Temperature
        <- rtn as Boolean: degrees < other.degrees

      operator == as pure
        -> other as Temperature
        <- rtn as Boolean: degrees == other.degrees

      operator #? as pure
        <- rtn as Integer: #? degrees

      operator $ as pure
        <- rtn as String: $degrees

      default operator ?

  defines program

    CompareTemps()
      stdout <- Stdout()
      hot <- Temperature(35.0)
      cold <- Temperature(5.0)
      if cold < hot
        stdout.println("cold is less than hot")

Common mistakes

E07520 — The < operator must return a Boolean but the return declaration is missing. Comparison operators have a specific return type requirement enforced by the compiler. See ek9 -h E07520 for details.

Incorrect:

      operator < as pure
        -> other as Temperature

Correct:

      operator < as pure
        -> other as Temperature
        <- rtn as Boolean: degrees < other.degrees

E07550 — The #? (hashcode) operator must return an Integer but the return declaration is missing. See ek9 -h E07550 for details.

Incorrect:

      operator #? as pure

Correct:

      operator #? as pure
        <- rtn as Integer: #? degrees

E07570 — The $ (string) operator must return a String but the return declaration is missing. See ek9 -h E07570 for details.

Incorrect:

      operator $ as pure

Correct:

      operator $ as pure
        <- rtn as String: $degrees

E06280 — The < operator takes exactly one parameter but two were provided. Comparison operators must accept one argument of the type being compared. See ek9 -h E06280 for details.

Incorrect:

      operator < as pure
        ->
          other as Temperature
          extra as Integer
        <- rtn as Boolean: degrees < other.degrees

Correct:

      operator < as pure
        -> other as Temperature
        <- rtn as Boolean: degrees < other.degrees

E06290 — The == operator requires one parameter but none was provided. Comparison operators must accept one argument of the type being compared. See ek9 -h E06290 for details.

Incorrect:

      operator == as pure
        <- rtn as Boolean: true

Correct:

      operator == as pure
        -> other as Temperature
        <- rtn as Boolean: degrees == other.degrees
Other ways to ask this
  • What triggers E07400 returning missing on an operator?
  • How do I define an operator with a return type?
  • Why does my comparison operator need a return declaration?

Coming from another language?

Java: operator overloading not supported (except + for String). Python: __lt__ must return a value, no enforcement on type. Rust: Ord trait methods have explicit return types. Kotlin: operator fun compareTo returns Int. Go: no operator overloading. EK9: operators have strict return type requirements enforced at compile time.

Keywords: return, missing, E07400, declaration, type, value, comparison, operator, boolean