Why can EK9 programs only return Integer?
← Syntax and Structure Rules · Ref: Q888
EK9 programs can only return Integer (or nothing). The return value becomes the process exit code, which the operating system uses to determine success or failure.
EXIT CODE CONVENTION
0 — success 1 — general error 2 — usage error Non-zero — failure
CORRECT PATTERNS
MyProgram()
<- exitCode as Integer: 0 // Returns exit code
MyProgram()
... body ... // No return (implicit 0)
INCORRECT PATTERNS
defines program MessageProgram() <- message as String: "done" // ERROR: E07590 SuccessProgram() <- success as Boolean: true // ERROR: E07590
WHY ENFORCED
The OS can only receive integer exit codes. Returning Float, Boolean, or custom types has no meaning at the OS level. EK9 makes this explicit rather than silently discarding non-integer values.
EVERY CODE IS YOURS
A program may return any Integer, including the numbers the ek9 command uses for its own failures (3, 8, 11 ...), so a script being replaced keeps the codes its callers expect. When EK9 itself fails - a compile error, or arguments that do not fit the program - its first stderr line says so: ek9: compilation failed (exit 8). Your program's exit never carries that line, so a caller tells the two apart from stderr, not from the number.
OTHER WAYS A PROGRAM'S STATUS IS SET
An unset return exits 1. An Exception nothing catches exits with its own exit code (or 1). A Panic, such as a failed require, always exits 1. See Q1431.
See Q3 for printing output. See Q872 for program variables.
Example
defines module qa.syntaxrules.programreturn defines program ProgramReturnIntegerDemo() <- exitCode as Integer: 0 stdout <- Stdout() stdout.println("Program running successfully") stdout.println(`Exit code will be: ${exitCode}`)
Common mistakes
E07590 — Programs can only return Integer because the return value is the process exit code. Float, Boolean, String, and custom types cannot be used as exit codes. See ek9 -h E07590 for details.
Incorrect:
<- exitCode as Float: 0.0
Correct:
<- exitCode as Integer: 0
Other ways to ask this
- What triggers E07590 PROGRAM_CAN_ONLY_RETURN_INTEGER?
- Why can't my program return a String or Boolean?
- How do exit codes work in EK9 programs?
Coming from another language?
Java: main returns void, exit code via System.exit(int). C: main returns int (exit code). Python: sys.exit(int) or implicit 0. Go: os.Exit(int). Rust: main returns () or Result. EK9: program return type restricted to Integer at compile time.
Keywords: E07590, code, Integer, exit, entry, process, main, point, program, return