Lyricdev
GitHub

LYR-SEM0169

a generic type's constant is not a constant expression

error ยท spec 04-modules

A static let of a generic type or generic block has an initializer that would run (a call, an operation), or names itself directly or through another such constant.

Explanation

A constant of a generic type is one per instance and folded where it is read; nothing runs for it before the entry. So its value is a constant: a literal, null, an empty literal, another constant, a unit variant, or a struct initializer of these. Such constants have no order among themselves, so none may name itself, directly or through another.

Write the value as a constant, or turn it into a static fn that computes it.

Wrong

fn make(): int {
    return 3;
}

struct Crate<T> {
    v: T,
    static let made: int = make();
}

fn main(): void {
    let _ = Crate<int>.made;
}
struct Crate<T> {
    v: T,
    static let made: int = 3;
}

fn main(): void {
    let _ = Crate<int>.made;
}