#Comptime Switch

1 messages · Page 1 of 1 (latest)

teal elm
#

Is there a way to have a switch that'll only compile the resulting block at comptime? I'm not sure if switch (comptime foo) does this without actually checking the value at execution.

#

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() {}

little arrow
#
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

teal elm
little arrow
#

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

teal elm
#

What if I select a target, because I plan to cross-compile to Windows and Mac OS.

little arrow
#

yes, the cross-compile target os

teal elm
#

I guess I should try it and see!

little arrow
#

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

teal elm
little arrow
#

you can compile the same program that runs on windows from windows, linux, or mac and the program only knows that it was compiled for windows

#

and in theory you get the exact same executable in each case