Lyricdev
GitHub

LYR-SEM0106

an initializer that omits a field without a default

error ยท spec 03-types

A struct, class or enum-variant initializer leaves out a field that has no default; all missing fields are named in one message.

Explanation

An initializer gives every field a value, except those with a default (level: int = 1) and optional fields, whose default is null without saying so. There is no implicit zero value for the rest: an omitted hp: int would have to be invented by the compiler.

Give the missing fields in the initializer, or declare a default for them in the type.

Wrong

import std.io { println };

struct Player { name: string, hp: int, level: int = 1 }

fn main(): void {
    let p = Player { name = "ada" };
    println(f"{p.hp}");
}
import std.io { println };

struct Player { name: string, hp: int, level: int = 1 }

fn main(): void {
    let p = Player { name = "ada", hp = 10 };
    println(f"{p.hp}");
}