Lyricdev
GitHub

LYR-SEM0161

an associated type's answer carries a bound

error ยท spec 05-interfaces

A conforming type answers an associated type with a bound of its own, as in type Item :: [Named] = Cat;.

Explanation

The bound of an associated type belongs to the interface that declares it: the interface says what every answer conforms to, and the answer is checked against that (LYR-SEM0160). An answer only names the type; a bound written there would say nothing the interface did not, or contradict it.

Write the answer as type Item = Cat;, and put the bound on the interface's declaration.

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 :: [Named] = Cat;
    fn item(): Cat { return Cat {}; }
}

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();
}