I used := to create a variable and got an error. Why?

← Getting Started · Ref: Q1040

In EK9, := NEVER creates a variable. It only updates one that already exists.

The rule is simple:

  <- creates a new variable (declaration)
  := updates an existing variable (reassignment)

They are completely separate operations. There is no overlap.

BEFORE (error):

  count := 10           error: count does not exist yet

AFTER (correct):

  count <- 10           creates count with value 10
  count := 20           later: updates count to 20

This is different from Python where := (walrus) creates AND assigns inside expressions. In EK9, := is a plain assignment statement that requires the variable to already exist.

This is also different from Go where := creates a new variable. In EK9, := never creates — only <- creates.

See Q1041 for Python developers. See Q1042 for Go developers. See Q1029 for a quick fix example. See Q965 for the full assignment operators guide.

Example

defines module qa.gettingstarted.assignnevercreates

  defines program

    AssignNeverCreatesDemo()
      stdout <- Stdout()

      // <- creates the variable
      count <- 10
      stdout.println(`Created: ${count}`)

      // := updates the existing variable
      count := 20
      stdout.println(`Updated: ${count}`)

      // Another example
      message <- "hello"
      message := "goodbye"
      stdout.println(message)

Common mistakes

E50001 — The := operator requires the variable to already exist. Use <- to create a new variable. In EK9, := never creates — it only reassigns.

Incorrect:

      count := 10

Correct:

      count <- 10
Other ways to ask this
  • Why can't I use := to make a new variable in EK9?
  • The compiler says my variable doesn't exist but I used :=
  • In Python := creates a variable. Why doesn't it work in EK9?

Coming from another language?

Python := (walrus) creates and assigns in expressions. Go := creates new variables. EK9 := does neither — it ONLY updates existing variables. Use <- to create.

Keywords: error, declare, walrus, go, python, assign, create