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 declare a return (omitting it is E07400); the return type is the author's choice
- 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 declares its sameness, <=> (returning Integer); <, ==, <> and the rest with another Temperature are generated from it and are never written by hand (E07181). It also defines < and == against an unrelated type (Float), each with a Boolean return declaration. 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 //The sameness: <, <=, ==, <>, >, >= with another Temperature are generated from it operator <=> as pure -> other as Temperature <- rtn as Integer: degrees <=> other.degrees //Comparisons with an unrelated type (Float) are written by hand operator < as pure -> threshold as Float <- rtn as Boolean: degrees < threshold operator == as pure -> threshold as Float <- rtn as Boolean: degrees == threshold //Arithmetic operators must declare a return; the type is the author's choice operator + as pure -> delta as Float <- rtn as Float: degrees + delta 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. (Here '<' compares with an unrelated type, Float; '<' with another Temperature is generated from '<=>' and is never written by hand - E07181.) Comparison operators have a specific return type requirement enforced by the compiler. See ek9 -h E07520 for details.
Incorrect:
operator < as pure -> threshold as Float
Correct:
operator < as pure -> threshold as Float <- rtn as Boolean: degrees < threshold
E07400 — The + operator must declare a return, but the return declaration is missing. Arithmetic operators have no fixed return type - the type is your choice - but they must return something. See ek9 -h E07400 for details.
Incorrect:
operator + as pure -> delta as Float
Correct:
operator + as pure -> delta as Float <- rtn as Float: degrees + delta
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 exactly one argument. See ek9 -h E06280 for details.
Incorrect:
operator < as pure -> threshold as Float extra as Integer <- rtn as Boolean: degrees < threshold
Correct:
operator < as pure -> threshold as Float <- rtn as Boolean: degrees < threshold
E06290 — The == operator requires one parameter but none was provided. Comparison operators must accept exactly one argument. See ek9 -h E06290 for details.
Incorrect:
operator == as pure <- rtn as Boolean: true
Correct:
operator == as pure -> threshold as Float <- rtn as Boolean: degrees == threshold
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: type, declaration, E07400, missing, return, boolean, operator, comparison, value