GALA-E0094 — Default value reads a later parameter

When it fires. A parameter’s default value reads its own parameter or one declared after it. A default is evaluated in the callee’s scope, left to right, with only the parameters before it bound, so a later parameter has no value yet. A field default of a struct or a sealed case may read no field at all (GALA-E0098). The error is reported at the declaration, whether or not any call leaves the argument out. A default that binds names of its own (a lambda, a match, a block), or reads the parameter only inside an interpolated string, is checked at a call that leaves the argument out, where it is evaluated.

Minimal repro.

package main

func area(w int = h, h int = 1) int = w * h

func main() {
    Println(area())
}

Error output.

error[GALA-E0094]: the default value of parameter w reads parameter h, which is not bound until after it
  --> main.gala:3:19
  |
3 | func area(w int = h, h int = 1) int = w * h
  |                   ^ a default may read only the parameters before its own
  |
  = hint: a default may read only the parameters before its own; declare h before w, or pass w explicitly

Fix. Declare the parameter the default reads first, or pass the argument explicitly:

package main

func area(h int = 1, w int = h) int = w * h

func main() {
    Println(area())
}

Rationale. GALA evaluates a call’s arguments left to right, then the defaults of the parameters it leaves out, as Scala and Kotlin do. A default that read a later parameter would see a value that does not exist yet.