Lyricdev
GitHub

LYR-SEM0160

an associated type's answer does not meet its bound

error ยท spec 05-interfaces

A conforming type (or an extend block) answers an associated type with a type that does not conform to the bound the interface declared for it, such as type Item :: [Named]; answered with int.

Explanation

An interface that declares type Item :: [Named]; promises every user of Self.Item that it is Named; generic code relies on that without knowing the answer. So every answer conforms to the bound, checked where the answer is given.

Answer with a type that conforms to the bound, or make the type conform to it.

Wrong

interface Named {
    fn name(): string;
}

struct Cat :: [Named] {
    fn name(): string { return "cat"; }
}

interface Holder {
    type Item :: [Named];
    fn item(): Self.Item;
}

struct Crate :: [Holder] {
    type Item = int;
    fn item(): int { return 1; }
}

fn main(): void {
    let _ = Crate {}.item();
    let _ = Cat {}.name();
}
interface Named {
    fn name(): string;
}

struct Cat :: [Named] {
    fn name(): string { return "cat"; }
}

interface Holder {
    type Item :: [Named];
    fn item(): Self.Item;
}

struct Crate :: [Holder] {
    type Item = Cat;
    fn item(): Cat { return Cat {}; }
}

fn main(): void {
    let _ = Crate {}.item();
    let _ = Cat {}.name();
}