#how to inline matches in switch based on some condition

1 messages · Page 1 of 1 (latest)

modern urchin
#

Hi,
I would like to somehow express something like this in Zig, but I'm struggling:

const y = switch(c) {
  inline if (doesSatisfyArbitraryCondition(c)) => |x| comptime f(x),
  else => somethingElseNotInlined,
};

is there some way to express this idea?
I have tried, for example:

pub fn f(x:u8) u8 {
    std.debug.assert(x>3 and x<6);
    return x;
}

pub fn g(x:u8) u8 { 
    const r = if(x>3 and x<6) 0 else switch(x){
        inline else => |y| comptime f(y),
    };
    return r;
}

but this does not work as it tries to inline everything in the switch, even though not all branches are going to be reachable.

little crow
#

you can't really do that, you have to itemize them or do it in the inline branch

switch (c) {
    4...5 => 0,
    inline else => |value| doSomething(value)
}
// or
switch (c) {
    inline else => |value| {
        if (value > 3 and value < 6) {
   
        } else {

        }
    }
}