Show me a practical EK9 example using both <- and := together.

← Getting Started · Ref: Q974

Here is a practical example showing <- for first declaration and := for subsequent updates. The pattern is always: <- once to create, then := (or : or =) to update.

The function declares 'total' with <- then updates it with := inside the loop. The program declares variables with <- and passes them to the function.

Example

defines module qa.gettingstarted.arrowequals

  defines function

    <?-
      Sums a list of integers.
      Uses <- to declare, := to update.
    -?>
    sumList()
      -> numbers as List of Integer
      <- rtn as Integer: 0

      for number in numbers
        rtn := rtn + number

  defines program

    ArrowEqualsDemo()
      stdout <- Stdout()

      // <- declares new variables
      scores <- [85, 92, 78, 95, 88]
      total <- sumList(scores)
      stdout.println(`Total: ${total}`)

      // := updates existing variable
      label <- "Scores"
      label := label + " (5 items)"
      stdout.println(label)

      // :=? only assigns if unset
      fallback <- String()
      fallback :=? "No scores available"
      stdout.println(`Fallback: ${fallback}`)

Common mistakes

E50001 — The first use of 'total' must declare it with <- ; using := on a variable that does not yet exist leaves it unresolved. See ek9 -h E50001 for details.

Incorrect:

total := sumList(scores)

Correct:

total <- sumList(scores)
Other ways to ask this
  • How do <- and := work together in a real EK9 program?
  • Show me EK9 code where I declare with <- then update with :=
  • Can you demonstrate the <- then := pattern in EK9?
  • What does a typical EK9 function look like with <- and :=?

Coming from another language?

Go: total := 0 then total = total + item. Python: total = 0 then total = total + item (ambiguous). EK9: total <- 0 then total := total + item (explicit separation).

Keywords: loop, declare, practical, update, assign