Lyricdev
GitHub

LYR-SEM0027

a member of a type parameter that no constraint provides

error ยท spec 05-interfaces

A generic body uses a member on a value of a type parameter (or of an associated type) that none of the parameter's constraints, and no blanket block they admit, declares.

Explanation

A generic function is checked once, for every type it may be instantiated with. Inside it, a value of T has only the members its constraints promise; that some instantiation's type has the member does not count.

Constrain the parameter by an interface that declares the member (<T :: [Show]>).

Wrong

interface Show { fn show(): string; }

fn label<T>(x: T): string { return x.show(); }

fn main(): void {
}
interface Show { fn show(): string; }

fn _label<T :: [Show]>(x: T): string { return x.show(); }

fn main(): void {
}