How do I declare multiple variables at once?

← Getting Started · Ref: Q28

EK9 declares one variable per line. There is no multi-variable declaration, no tuple unpacking, and no destructuring. This is a deliberate design decision for clarity and readability.

LOCAL VARIABLES: ONE PER LINE

  count <- 42
  name <- "Steve"
  ratio <- 3.14
  ready <- true

Each variable gets its own line with its own <- declaration. There is no syntax for count, name, ratio <- 42, "Steve", 3.14 or anything similar.

FUNCTION PARAMETERS: MULTIPLE BUT SEPARATE LINES

When a function takes multiple parameters, each is declared on its own line in a -> block:

  calculate()
    ->
      width as Float
      height as Float
    <- area as Float: width * height

The single-parameter shorthand puts it on the same line as ->:

  square()
    -> side as Float
    <- area as Float: side * side

MULTIPLE RETURN VALUES

EK9 does not have tuple unpacking like Python's a, b = get_pair(). Instead, use a record or class to group related return values:

  defines record
    Outcome
      message <- String()
      exitCode <- Integer()
      default operator ?
  processData()
    -> inputData as String
    <- outcome as Outcome: Outcome()

The caller accesses fields by name: result.message, result.exitCode. This is more readable than positional tuple unpacking because every value has a name.

WHY NO DESTRUCTURING?

Destructuring and tuple unpacking introduce positional coupling. In Python, a, b, c = get_values() breaks silently if the function starts returning four values or if someone swaps the order. Named fields make the contract explicit and changes visible.

OTHER LANGUAGES

  Python: a, b = 1, 2 and a, b, c = get_triple() with positional unpacking
  Go: var (a int; b string) blocks and a, b := getTwoValues() multi-return
  Rust: let (a, b) = (1, 2) destructuring and pattern matching
  JavaScript: const {a, b} = obj and const [x, y] = arr
  Kotlin: val (a, b) = Pair(1, 2) with componentN() convention

EK9 avoids all of these. One variable per line, named fields for grouped values, explicit and readable. The two extra lines you write are repaid every time someone reads the code.

See Q29 for unset variables. See Q22 for declaring variables.

Example

defines module qa.multiple.variables

  defines record
    Outcome
      message <- String()
      exitCode <- Integer()
      default operator ?

  defines function

    processData()
      -> inputData as String
      <- outcome as Outcome: Outcome()
      require inputData?

  defines program
    MultipleVariablesDemo()
      stdout <- Stdout()

      // One variable per line
      count <- 42
      name <- "Steve"
      ratio <- 3.14
      ready <- true

      stdout.println(`${name}: ${count} ${ratio} ${ready}`)

      // Grouped return values via record
      result <- processData("test")
      stdout.println(`Message: ${result.message}`)
      stdout.println(`Exit code: ${result.exitCode}`)

Common mistakes

E08180 — Record fields must be initialised inline. An uninitialised field triggers E08180. Use a constructor call like String() for an unset default. See ek9 -h E08180 for details.

Incorrect:

message as String

Correct:

message <- String()

E07520 — The ? operator is inherited from the base type and requires 'default' for auto-generation or 'override' for custom implementation. Declaring bare 'operator ?' triggers E07520 because operator semantics require a Boolean return. See ek9 -h E07520 for details.

Incorrect:

operator ?

Correct:

default operator ?
Other ways to ask this
  • Does EK9 have tuple unpacking or destructuring?
  • Can I declare several variables on one line in EK9?
  • How do I return multiple values from a function in EK9?

Coming from another language?

Python: a, b = 1, 2 tuple unpacking, multiple return values. Go: var (...) blocks, a, b := f() multi-return. Rust: let (a, b) destructuring, pattern matching. JavaScript: const {a, b} = obj, const [x, y] = arr. Kotlin: val (a, b) = Pair with componentN(). EK9: one variable per line, no destructuring, no tuple unpacking. Use records for grouped return values with named fields.

Keywords: tuple, declare, migrate, intro, destructuring, multiple, record, beginner, unpacking, return, first, several, variable, many, group, start