Lyricdev
GitHub

LYR-PAR0061

a 'comptime if' in expression position without 'else'

error ยท spec 11-metaprogramming

A comptime if (c) a stands where a value is taken and has no else branch.

Explanation

In expression position a comptime if chooses between two values at compile time; without else there would be no value when the condition is false.

Write the else branch.

Wrong

import std.meta { fields };

struct Point { x: int, y: int }

fn main(): void {
    var n = 0;
    comptime for (f in fields<Point>()) {
        n = n + comptime if (f.name == "x") 1 2;
    }
    let _ = n;
}
import std.meta { fields };

struct Point { x: int, y: int }

fn main(): void {
    var n = 0;
    comptime for (f in fields<Point>()) {
        n = n + comptime if (f.name == "x") 1 else 2;
    }
    let _ = n;
}