Lyricdev
GitHub

LYR-RES0005

modules that import each other

error ยท spec 04-modules

The imports of the program's modules form a cycle: a module imports, directly or through others, a module that imports it.

Explanation

Imports form no cycle. A module's names and its initializers depend on what it imports, and a cycle would leave no order in which they could be ready.

Move what both modules need into a third module that both import, or let the dependency run in one direction only.

Wrong

//- src/main.lyr
import app.first { one };

fn main(): void {
    assert(one() == 2);
}
//- src/first.lyr
import app.second { two };

pub fn one(): int {
    return two();
}
//- src/second.lyr
import app.first { one };

pub fn two(): int {
    return 2;
}

pub fn again(): int {
    return one();
}
//- src/main.lyr
import app.first { one };

fn main(): void {
    assert(one() == 2);
}
//- src/first.lyr
import app.second { two };

pub fn one(): int {
    return two();
}
//- src/second.lyr
pub fn two(): int {
    return 2;
}