Why does AI generate return statements in EK9?
← What AI Gets Wrong About EK9 · Ref: Q274
AI language models trained on Java, Python, and Rust confidently generate return statements in EK9. This is one of the most common AI hallucinations because every mainstream language uses return. EK9 has NO return keyword. It does not exist in the grammar.
THE AI MISTAKE
AI generates code like 'return result', 'return "hello"', 'return;', or early return patterns like 'if invalid then return'. None of these compile in EK9. The keyword does not exist.
THE EK9 WAY
Declare a named return variable with '<-' and the compiler ensures all code paths initialise it:
greet() as pure -> name as String <- message as String: "Hello, " + name
The return variable 'message' is declared, initialised, and automatically returned. No return statement needed.
CONDITIONAL RETURNS
When you need different values on different paths, declare a default then modify:
classify() as pure -> score as Integer <- label as String: "average" if score >= 90 label: "excellent" else if score < 40 label: "poor"
The compiler verifies every path initialises 'label'. No early return needed.
REPLACING EARLY RETURN
AI often generates early return for precondition checks. In EK9, use structured control flow:
process() as pure -> items as List of String <- count as Integer: 0 if ~items? count: -1 else count: length items
All paths explicitly set the return variable. The compiler enforces completeness.
GUARD EXPRESSIONS
For conditional processing where you only proceed if a value is set:
lookup()
-> key as String
<- found as String: String()
if record <- findRecord(key)
found: $record
The guard 'if record <- findRecord(key)' checks isSet and assigns in one step. If unset, the block is skipped.
WHY REMOVED
Return statements create hidden exit points. Functions with multiple returns are harder to reason about, harder to debug, and produce unreachable code. Named return variables make every path explicit and verifiable.
See Q50 for declared return variables. See Q144 for why return was removed. See Q281 for verifying AI code. See Q289 for before and after migration patterns.
Example
defines module qa.ai.mistakes.returnstatement defines function greet() as pure -> name as String <- message as String: "Hello, " + name classify() as pure -> score as Integer <- label as String: "average" excellentThreshold <- 90 poorThreshold <- 40 if score >= excellentThreshold label: "excellent" else if score < poorThreshold label: "poor" countItems() as pure -> items as List of String <- count as Integer: 0 if ~items? count: -1 else count: length items defines program ReturnMistakeDemo() stdout <- Stdout() // === SIMPLE RETURN === stdout.println(greet("Steve")) // === CONDITIONAL RETURN === stdout.println(`Score 95: ${classify(95)}`) stdout.println(`Score 30: ${classify(30)}`) stdout.println(`Score 60: ${classify(60)}`) // === REPLACING EARLY RETURN === names <- ["Alice", "Bob"] stdout.println(`Count: ${countItems(names)}`) emptyList <- List() of String stdout.println(`Empty count: ${countItems(emptyList)}`)
Common mistakes
E01072 — EK9 has no return keyword. Declare a named return variable with '<-' instead. The compiler enforces all code paths initialise it. See ek9 -h E01072 for details.
Incorrect:
return "Hello, " + name
Correct:
<- message as String: "Hello, " + name
E01072 — EK9 uses declared return variables, not return statements. Assign different values on different code paths and the compiler verifies completeness. See ek9 -h E01072 for details.
Incorrect:
return "average"
Correct:
<- label as String: "average"
E01072 — AI generates early return for conditional paths. In EK9, assign to the declared return variable instead. The compiler verifies all paths initialise it. No early exit needed. See ek9 -h E01072 for details.
Incorrect:
return "excellent"
Correct:
label: "excellent"
Other ways to ask this
- Why does my AI assistant use return in EK9 code?
- How do I fix AI-generated return statements in EK9?
- What replaces the return keyword in EK9?
Coming from another language?
Java: return statement required, early return common for preconditions. Python: return statement, implicit None if omitted. Rust: implicit last expression or explicit return. Go: named returns exist but return still required. JavaScript: return statement, undefined if omitted. EK9: NO return statement, named return variable with '<-', compiler verifies all paths, guard expressions replace early return.
Keywords: variable, exit, pitfall, wrong, hallucination, early, common-error, ai, return, migrate, precondition, statement, declare, mistake