#Comptime Switch
1 messages · Page 1 of 1 (latest)
I guess this is the wrong question to ask, what I'm more looking for is the ability to write this Rust code in Zigrust #[cfg(unix)] fn a() {} #[cfg(windows)] fn a() {}
const builtin = @import("builtin");
fn a() {
if (builtin.os.tag == .windows) {
// windows specific
} else {
// other implementation
}
}
if the condition of an if or switch is known at comptime, then only the corresponding prong will be evaluated
So in this case, would it be preferrable to check the target os rather than the os?
I'm not sure what distinction you are making
builtin.os.tag is the operating system that your program is being compiled to run on
What if I select a target, because I plan to cross-compile to Windows and Mac OS.
yes, the cross-compile target os
I guess I should try it and see!
there's no way to know in a program what os it was compiled on
you could set build options from a build script if you really needed to
I'm unsure what you mean?