I used := to create a list. How do I fix it?

← Getting Started · Ref: Q1033

Change := to <-. Use <- for first-time declarations, including lists.

BEFORE (wrong):

  names := ["Alice", "Bob", "Charlie"]

AFTER (fixed):

  names <- ["Alice", "Bob", "Charlie"]

For an empty list, declare the type explicitly:

  names <- List of String()

For a list with items, the type is inferred from the contents:

  numbers <- [1, 2, 3]         List of Integer
  prices <- [9.99, 12.50]      List of Float

The := operator is for updating an existing variable:

  names <- ["Alice"]           create with <-
  names := ["Alice", "Bob"]    update with :=

Example

defines module qa.gettingstarted.fixlistcreation

  defines program

    FixListDemo()
      stdout <- Stdout()

      //Correct: <- to create a new list
      names <- ["Alice", "Bob", "Charlie"]
      stdout.println(`Names: ${names}`)

      //Correct: := to update an existing list
      names := ["Alice", "Bob", "Charlie", "Dave"]
      stdout.println(`Updated: ${names}`)

      //Empty list with explicit type
      scores <- List() of Integer
      stdout.println(`Empty scores: ${scores}`)

Common mistakes

E50001 — Use <- to declare a new variable. The := operator expects the variable to already exist.

Incorrect:

      names := ["Alice", "Bob", "Charlie"]

Correct:

      names <- ["Alice", "Bob", "Charlie"]
Other ways to ask this
  • Quick fix: used := instead of <- for a new list
  • How do I declare a new list in EK9?
  • Fix my list creation: names := [Alice, Bob]

Coming from another language?

EK9 uses <- for all first-time declarations, including list literals. Use := only to reassign.

Keywords: collection, quick, create, fix, list, declare