Lyricdev
GitHub

LYR-SEM0168

a type's member hides a blanket member

warning · spec 05-interfaces · lint blanket-shadowing, group correctness, warn unless said

A type has a member of its own (written, inherited as an interface default, or delegated to a field) with the same name as a member a blanket extend<T :: […]> T block gives it.

Explanation

A call on the type reaches its own member; generic code that reaches the name through the block's constraints calls the block's. Two functions of one name then exist for the type, and which one runs depends on how the call is reached.

Rename one of them, unless the two are meant to differ.

Wrong

interface Named {
    fn name(): string;
}

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

    fn greeting(): string {
        return "meow";
    }
}

extend<T :: [Named]> T {
    fn greeting(): string {
        return "hello " + this.name();
    }
}

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

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

    fn sound(): string {
        return "meow";
    }
}

extend<T :: [Named]> T {
    fn greeting(): string {
        return "hello " + this.name();
    }
}

fn main(): void {
    let _ = Cat {}.greeting();
    let _ = Cat {}.sound();
}