I want to have a better API for my comptime parsing. The only way I have right now is to have a separate method for comptime, and then passing undefined into where the allocator would go, and try to communicate to the parsing function that the allocator shouldn't be used.
pub const Mode = enum(u1) {
compile_time,
run_time,
};
fn parseImpl(comptime mode: Mode, allocator: Allocator, ...) Parsed {
...
}
pub fn parseComptime(...) Parsed {
return parseImpl(.compile_time, undefined, ...);
}
pub fn parseRuntime(allocator: Allocator, ...) Parsed {
return parseImpl(.run_time, allocator, ...);
}
I was wondering if there was a way to have compile time union tags, but potential runtime data, so like:
pub const ParseMode = union(enum(u1)) {
compile_time: void,
run_time: std.mem.Allocator,
};
in which case I wouldn't have to use undefined:
fn parseImpl(mode: Mode, ...) Parsed {
...
}
pub fn parseComptime(...) Parsed {
return parseImpl(.compile_time, ...);
}
pub fn parseRuntime(allocator: Allocator, ...) Parsed {
return parseImpl(.{ .run_time = allocator }, ...);
}
this doesn't work, as mode is not compile time known, so the compile time logic will be analysed for runtime. what's the best way to handle this situation?