Lyricdev
GitHub

LYR-SEM0057

a global read before it is initialized

error ยท spec 04-modules

The initializer of a module-level binding reads a global of the same module that is declared after it.

Explanation

Globals are initialized module by module in import order, and within a module in declaration order. An initializer may read what its module declared earlier and anything from a module it imports; a later global has no value yet. Function bodies are not affected: they run after all globals are initialized.

Move the declaration that is read above the one that reads it.

Wrong

import std.io { println };

let total: int = base + 1;
let base: int = 41;

fn main(): void {
    println(total);
}
import std.io { println };

let base: int = 41;
let total: int = base + 1;

fn main(): void {
    println(total);
}