Let's say there's an enum like this:
const Block = enum {
a, b, c, d,
fn model(self: @This()) Model {
return switch (self) {
b, c, d => .cube,
else => .none,
};
}
fn isFullbright(self: @This()) bool {
return switch (self) {
d => true,
else => false,
};
}
};
Is it a good idea to make generic functions with switch statements like this? What I wonder is if the compiler is smart enough to optimize the many enum checks. Else I would have to write functions which contain only a single switch, but that doesn't feel nice.
fn generateMesh() void {
// foreach block in volume
const model = block.model();
const is_fullbright = block.isFullbright();
}
Or like this if it can't optimize it.
fn generateMesh() void {
// foreach block in volume
const model, const is_fullbright = expr: switch (block) {
a => break :expr .{.none, false},
b, c => break :expr .{.cube, false},
d => break :expr .{.cube, true},
}
}