I come from Python. I keep using := where I should use <-. How do I remember the difference?

← Getting Started · Ref: Q1041

The Python habit to break: in Python, := (walrus) creates a variable inside an expression. In EK9, := NEVER creates anything.

Simple rule: first time you mention a name, use <-. Every time after, use :=.

  name <- "Alice"       FIRST mention: <- to create
  name := "Bob"         SECOND mention: := to update

Think of <- as 'born here' and := as 'changed here'.

Python patterns mapped to EK9:

  Python: x = 10              EK9: x <- 10        (first use)
  Python: x = 20              EK9: x := 20        (update)
  Python: if (m := re.match)  EK9: if m <- match() (guard creates m)
  Python: [x := f(y)]         EK9: not needed — use stream pipeline

The compiler catches every mistake. If you use := on a name that does not exist, the compiler tells you. If you use <- on a name that already exists, the compiler tells you. You cannot get it wrong without knowing.

See Q1040 for the general rule. See Q986 for walrus vs EK9 assign. See Q1039 for <- vs :=? distinction.

Example

defines module qa.gettingstarted.pythonassigntrap

  defines function

    findMatch() as pure
      -> text as String
      <- rtn as String: String()

      minLength <- 3
      if text.length() > minLength
        rtn: text

  defines program

    PythonTrapDemo()
      stdout <- Stdout()

      // <- creates (like Python's first x = ...)
      greeting <- "Hello"
      stdout.println(greeting)

      // := updates (like Python's second x = ...)
      greeting := "Hi there"
      stdout.println(greeting)

      // <- in guard (replaces Python's walrus := in if)
      if match <- findMatch("EK9 language")
        stdout.println(`Found: ${match}`)

Common mistakes

E50001 — In EK9 ':=' only updates an existing variable; use '<-' the first time you introduce a name, or it is not resolved. See ek9 -h E50001 for details.

Incorrect:

greeting := "Hello"

Correct:

greeting <- "Hello"
Other ways to ask this
  • As a Python developer, how do I stop confusing := and <- in EK9?
  • Python habit: I write := for everything. What is the EK9 rule?
  • Help me break the Python := habit in EK9

Coming from another language?

Python developers: forget := walrus habits. EK9 <- is for creation, := is for update. The compiler enforces this — you cannot confuse them without an error.

Keywords: walrus, declare, habit, trap, migrate, python, assign