Lyricdev
GitHub

LYR-SEM0074

a static extension called on a value

error ยท spec 05-interfaces

A static function added by an extend block is called through a value instead of through the type.

Explanation

A static function belongs to the type and takes no receiver; calling it on a value suggests the value takes part, and it does not. The same holds for a static function an extension adds to a shape such as an array.

Call it through the type: Point.origin().

Wrong

struct Point {
    x: int,
}

extend Point {
    static fn origin(): Point { return Point { x = 0 }; }
}

fn main(): void {
    let p = Point { x = 1 };
    let _ = p.origin();
}
struct Point {
    x: int,
}

extend Point {
    static fn origin(): Point { return Point { x = 0 }; }
}

fn main(): void {
    let _ = Point.origin();
}