Language reference · section 7

7. Bindings

Feature: Bindings

  Scenario: A plain assignment makes an immutable binding
    Given the code "x = 1"
    When the code assigns "x = 2" in the same scope
    Then the compiler reports an error "`x` is an immutable binding and cannot be reassigned; declare it with `mut x = ...` to allow reassignment"

  Scenario: A mut declaration makes a mutable binding
    Given the code "mut x = 1"
    When the code assigns "x = 2" in the same scope
    Then x is 2

  Scenario: A name cannot be declared mut twice in one scope
    Given the code "mut x = 1" followed by "mut x = 2" in the same scope
    Then the compiler reports an error "`x` is already declared in this scope"

  Scenario: const is an immutable binding
    Given the code "const x = 1"
    Then x is an immutable binding

  Scenario: A binding can have a type
    Given the code "x: number = 1"
    Then x is a number binding

  Scenario: export takes the same forms as a binding
    Given the code "export mut x = 1"
    Then other modules can import x

  Scenario: An inner mut declaration shadows an outer name
    Given an outer name x
    When an inner block declares "mut x = 2"
    Then the inner block uses the new binding and the outer x does not change

  Scenario: An inner plain assignment cannot reassign a visible immutable name
    Given the code "x = 1" at top level, then "fn f():" with "x = 2" in its body
    Then the compiler reports an error "`x` is an immutable binding and cannot be reassigned; declare it with `mut x = ...` to allow reassignment"

  Scenario: A plain assignment to a name that is not visible declares a local binding
    Given the code "fn f():" with "x = 2" and "return x" in its body
    Then f() returns 2

  Scenario: A declaration cannot end with if
    Given the code "x = 2 if true"
    Then the compiler reports an error "a declaration cannot have a trailing `if`; write it as an `if:` block"