Lyricdev
GitHub

LYR-SEM0120

a parameter default that reads 'this'

error · spec 08-expressions

The default value of a method's parameter mentions 'this'.

Explanation

A default is evaluated at the call, before the receiver is bound — it is part of the caller's arguments, not of the method's body. It may read the parameters before it and module-level names, but not this.

Use a default that does not depend on the receiver (an optional parameter, by: ?int = null, resolved in the body), or compute the value in the body.

Wrong

struct Scale {
    factor: int,
    fn apply(x: int, by: int = this.factor): int { return x * by; }
}

fn main(): void {
    let _ = Scale { factor = 2 }.apply(3);
}
struct Scale {
    factor: int,
    fn apply(x: int, by: int = 1): int { return x * by * this.factor; }
}

fn main(): void {
    let _ = Scale { factor = 2 }.apply(3);
}