Lyricdev
GitHub

LYR-SEM0166

a spread argument is not the whole rest of a variadic call

error ยท spec 08-expressions

A call writes xs... where it does not stand for the variadic parameter's whole array: other arguments follow it or stand beside it in the variadic part, the call spreads twice, or the callee has no variadic parameter.

Explanation

f(xs...) hands the array xs to a variadic parameter as that parameter's array. A spread is therefore the rest of the arguments: it stands alone at the variadic position, nothing follows it, and a call has at most one.

Build the full array first and spread that, or pass the elements one by one.

Wrong

fn sum(xs: int...): int {
    var s = 0;
    for (x in xs) { s += x; }
    return s;
}

fn main(): void {
    let xs = [1, 2, 3];
    assert(sum(xs..., 4) == 10);
}
fn sum(xs: int...): int {
    var s = 0;
    for (x in xs) { s += x; }
    return s;
}

fn main(): void {
    let xs = [1, 2, 3];
    assert(sum(xs...) == 6);
}