Why does EK9 reject an operator that takes a parameter but has no return declaration?

← Functions and Methods · Ref: Q1319

Operators with value semantics (such as +, -, *, /, <=>, $, #?) MUST produce a value, so EK9 requires a return declaration in their signature. Omitting the '<- rtn as Type: ...' line raises E07400 (RETURNING_MISSING): the operator promises a result but never declares one, so any expression using it would have nothing to evaluate to.

EK9 has no return keyword - the return value is a named variable declared with '<-' in the signature. The compiler then enforces that all paths give it a value. To fix E07400, add the return declaration:

  operator + as pure
    -> other as Vector
    <- rtn as Vector: Vector(x + other.x)

Note this is different from mutating operators like ++ and -- which must NOT declare a return (that would raise RETURN_VALUE_NOT_SUPPORTED).

See Q1001 for the general '<- rtn' return mechanism.

Example

defines module qa.functionsandmethods.operatorreturn

  defines class

    Vector
      x as Integer: Integer()

      Vector() as pure
        -> initial as Integer
        x :=? initial

      //CORRECT: a value-producing operator declares its return with '<- rtn'.
      //Omitting this line would raise E07400 RETURNING_MISSING.
      operator + as pure
        -> other as Vector
        <- rtn as Vector: Vector(x + other.x)

      operator $ as pure
        <- rtn as String: `Vector(${x})`

      override operator ? as pure
        <- rtn as Boolean: x?

  defines program

    OperatorReturnDemo()
      stdout <- Stdout()

      a <- Vector(3)
      b <- Vector(4)
      sum <- a + b
      stdout.println(`Sum is ${sum}`)

Common mistakes

E07400 — A value-producing operator like '+' must declare what it returns, but this signature has only an incoming parameter and no '<- rtn as Type' line, so EK9 raises E07400 - the operator promises a result it never produces. Add the return declaration '<- rtn as Vector: ...' so all paths supply a value. (Mutating operators such as ++ and -- are the opposite: they must NOT declare a return.) See ek9 -h E07400 for details.

Incorrect:

operator + as pure
  -> other as Vector

Correct:

operator + as pure
        -> other as Vector
        <- rtn as Vector: Vector(x + other.x)
Other ways to ask this
  • What triggers E07400 RETURNING_MISSING on an operator?
  • Why must my '+' operator declare a '<- rtn' return value?
  • How do I fix 'returning variable and type missing' in EK9?

Coming from another language?

Java/Kotlin: operator overloads are just methods whose return type is part of the signature; forgetting to return compiles only if the declared return type is void, otherwise it is a normal type error. C++: an operator+ with a non-void return that never returns is undefined behaviour, often only a warning. EK9: the missing return is a hard compile-time error (E07400) at type-definition time, because the return value is a declared variable the compiler can see is absent.

Keywords: missing, operator, value, return, rtn, declaration, RETURNING_MISSING, E07400