Why does AI confuse declarations and assignments in EK9?
← What AI Gets Wrong About EK9 · Ref: Q276
AI models generate 'x = 5', 'let x = 5', 'var x = 5', or 'val x = 5' in EK9. None of these are valid EK9 declarations. AI conflates two distinct concepts: creating a new variable (declaration) and changing an existing variable (assignment).
THE AI MISTAKE
AI does not understand EK9's distinction between declaration and assignment. It imports syntax from JavaScript (let/const), Kotlin (val/var), Rust (let), or Java (type name = value).
EK9 DECLARATIONS
Create a new variable with '<-' (type inferred) or explicit type with ':' initialiser:
name <- "hello" age <- 42 score as Float: 9.5 greeting as String: "hi"
The '<-' operator creates AND initialises. The 'as Type:' form declares with explicit type.
EK9 ASSIGNMENTS
Assign to an EXISTING variable with ':', ':=', or '=':
name: "world" name := "world" name = "world"
All three have IDENTICAL semantics. The choice is stylistic:
- ':' is concise, natural in loop bodies, switch branches, if/else blocks
- ':=' is stronger, highlights significant or deliberate assignments
- '=' is also valid, same semantics as the other two
ASSIGN-IF-UNSET
The ':=?' operator has DIFFERENT semantics from the other three. It only assigns if the variable is currently UNSET:
config <- String() config :=? "default" config :=? "other"
After this, config is "default". The second ':=?' does nothing because config is already set.
THE COMPILER ENFORCES THE DISTINCTION
Using '<-' on an existing variable is an error. Using ':' or ':=' on a non-existent variable is an error. The compiler knows whether you are declaring or assigning.
See Q24 for variable declaration. See Q29 for assign-if-unset. See Q168 for fallback values. See Q243 for coalescing operators. See Q281 for verifying AI code.
Example
defines module qa.ai.mistakes.assignment defines program AssignmentDemo() stdout <- Stdout() // === DECLARATIONS WITH <- === name <- "Alice" age <- 30 active <- true stdout.println(`Declared: ${name}, ${age}, ${active}`) // === EXPLICIT TYPE DECLARATIONS === greeting as String: "Hello" limit as Integer: 100 stdout.println(`Typed: ${greeting}, ${limit}`) // === ASSIGNMENTS (all three identical) === name: "Bob" stdout.println(`After ':' assignment: ${name}`) name := "Charlie" stdout.println(`After ':=' assignment: ${name}`) name = "Diana" stdout.println(`After '=' assignment: ${name}`) // === ASSIGN-IF-UNSET :=? === config <- String() stdout.println(`Before guard: config is set? ${config?}`) config :=? "default" stdout.println(`After first guard: ${config}`) config :=? "other" stdout.println(`After second guard: ${config}`) // === STYLISTIC CHOICE IN CONTEXT === total <- 0 for num in [1, 2, 3, 4, 5] total: total + num stdout.println(`Loop total with ':': ${total}`) strategy <- "none" strategy := "aggressive" stdout.println(`Strategy with ':=': ${strategy}`)
Common mistakes
E50001 — Using '=' attempts to assign to an existing variable, but 'name' was never declared. In EK9, '<-' creates a new variable (declaration), while '=', ':=', and ':' assign to an EXISTING variable. AI trained on Python uses '=' for both, but EK9 enforces the distinction. See ek9 -h E50001 for details.
Incorrect:
name = "Alice"
Correct:
name <- "Alice"
Other ways to ask this
- Why does AI generate 'let x = 5' or 'var x = 5' in EK9?
- How do declarations and assignments differ in EK9?
- What are the EK9 assignment operators?
Coming from another language?
Java: 'Type name = value' for declaration, 'name = value' for assignment, same '=' symbol for both. JavaScript: 'let x = 5', 'const x = 5', 'x = 5'. Kotlin: 'val x = 5' (immutable), 'var x = 5' (mutable). Rust: 'let x = 5', 'let mut x = 5'. Python: 'x = 5' for both declaration and assignment. Go: ':=' for short declaration, '=' for assignment. EK9: '<-' for declaration (type inferred), 'as Type:' for explicit type, ':', ':=', '=' for assignment (all identical), ':=?' for assign-if-unset (different semantics).
Keywords: pitfall, variable, assignment, var, let, syntax, infer, operator, hallucination, equals, mistake, ai, declaration, wrong, migrate, declare, common-error