when
A when
statement is similar to an if
statement but its condition is evaluated at compile time. The condition must be a constant boolean.
when true {
fmt.println("This is called");
}
A when
condition can have an else
clause but its contents does not get checked unless the condition is false
.
when false {
this_is_valid(syntax);
but := "because";
it = is_never_evaluated;
it = never_gets_ran;
} else {
fmt.println("Hello");
}
A when
statement does create a new scope with its braces { }
unlike other flow control statements.
This is very useful for platform specific code.
when condition
In many cases, a library may want to separate OS specific code into separate file. To only import that file depend on a constant condition, a when
condition can be append to a hash directive.
#import win32 "sys/windows.odin" when ODIN_OS == "windows";
#import foo "foo_amd64.odin" when ODIN_ARCH == "amd64";
#import foo "foo_x86.odin" when ODIN_ARCH == "x86";
#load "bar.odin" when ODIN_ENDIAN == "big";
#foreign_system_library gl32 "opengl32.lib" when ODIN_OS == "windows";