Code for each platform
when, followed by a condition and do, keeps code for some platforms
only. The condition is decided when the program is compiled:
when platform("windows") do
const str SEPARATOR = "\\"
import "windows_paths" as paths
else when platform("macos") or platform("linux") do
const str SEPARATOR = "/"
import "posix_paths" as paths
else
const str SEPARATOR = "/"
end
func temp_name() str do
when platform("windows") do
str base = "TEMP"
else
str base = "TMPDIR"
end
return base
endA condition is made of these tests, joined with and, or, not, and
parentheses:
| Test | True when |
|---|---|
platform("linux") |
the program is built for that system: linux, macos, windows, freebsd, openbsd, or wasi |
arch("arm64") |
the program is built for that processor: x86_64, arm64, or wasm32 |
defined(NAME) |
-D NAME was given on the command line; see build constants |
true, false |
always, never |
The statements of the first branch whose condition holds, or of the else,
take the place of the whole when: they open no scope of their own, so a
variable or a declaration made in a branch is seen after it. The other
branches are parsed, so their syntax must be right, but they are not
checked, and their imports are not loaded, so they may use functions, types,
and modules that exist only on their platform. A type declared only in a
branch left out is unknown in the rest of the program.
when works at the top level, around declarations, constants, imports, and
statements, and inside functions, around statements. It is told apart from
a branch of a match by its do: a match branch ends in then.
A program is built for the system the compiler runs on, which platform()
and arch() also return when it runs. cuarzo check --platform windows
and cuarzo c --platform windows check or translate it as it would be on
another system, and --arch does the same for the processor; building a
program for another system needs that system's C compiler.