I keep getting errors mixing up <- and := in EK9. What is the rule?

← Getting Started · Ref: Q881

Simple rule: '<-' is for the FIRST TIME. ':=' is for EVERY TIME AFTER.

  score <- 100      // first time: score is born here
  score := 200      // after: score already exists, update it
  score <- 300      // ERROR E50050: score already exists

Using '<-' on a name that already exists - in the same scope or in any nested block - is compile error E50050 (Variable/Constant duplicated). EK9 has no shadowing, so this mistake can never silently create a second variable: the compiler rejects it and you change the line to ':='.

For function returns, '<-' declares the return variable:

  calculateTotal()
    -> items as List of Integer
    <- total as Integer: 0    // '<-' declares return
    for item in items
      total += item            // ':=' implicit via +=

See Q22 for declaration rules. See Q879 for all three assignment operators.

Example

defines module qa.getting.started.declare.vs.assign

  defines function

    sumList()
      -> numbers as List of Integer
      <- total as Integer: 0

      for item in numbers
        total += item

  defines program

    DeclareVsAssign()
      stdout <- Stdout()

      //Correct: declare then assign
      message <- "hello"
      message := "updated"
      stdout.println(message)

      //Demonstrate with a list
      scores <- List() of Integer
      scores += 10
      scores += 20
      scores += 30

      result <- sumList(scores)
      stdout.println(`Total: ${result}`)

Common mistakes

E50050 — message was already declared with '<-', so declaring it again is E50050 (Variable/Constant duplicated). EK9 never shadows a variable; use ':=' to change it. See ek9 -h E50050 for details.

Incorrect:

      message <- "updated"

Correct:

      message := "updated"
Other ways to ask this
  • When do I use the arrow <- vs the colon-equals := in EK9?
  • My variable already exists and <- gives E50050 — why?
  • Can I shadow a variable with the <- operator in EK9?

Coming from another language?

Python: rebinding with = is always reassignment. Java: type declaration vs assignment. EK9: <- declares, := reassigns — using <- twice on one name is compile error E50050 (no shadowing).

Keywords: error, scope, variable, declare, E50050, rebind, arrow, assign, shadow