std/core/target
std/core/src/target.trb
What a program is compiled for: OperatingSystem, Architecture and ByteOrder, each with one static current.
The three constants are parameters of the build and not questions to the machine: torb build --target sets them,
the host is the default, and the VM compiles a program on the machine it runs it on. A match on one of them keeps
the one arm its value selects, while every arm is still type checked on every machine (the language page
docs/language/execution/compile-time-branches.md). That is how code for one operating system is written in
TorbScript, next to the code for the others, without a declaration that exists on one target and not on another.
They are not in the prelude: a branch on the target is rare in application code, and the import is the statement "this file branches on where it runs".
type OperatingSystem
type OperatingSystem with Show, Equals, Hash
The operating systems the toolchain builds for. A case is added when a target is, and every
match OperatingSystem.current of a workspace is then a compile error that names the missing case.
Examples
fn searchPathSeparator(): String {
match OperatingSystem.current {
.Windows => ";"
.Linux | .MacOs | .FreeBsd => ":"
}
}
Pitfalls
- Adding a case is a breaking change of
std/core. A branch that must survive it writes_for "the others", or asksOperatingSystem.isPosix. const system = OperatingSystem.currentinside a function is an ordinary binding, and amatch systemcompiles every arm. Branch onOperatingSystem.currentitself where only one arm should be part of the program.
case Windows
case Windows
Microsoft Windows.
case Linux
case Linux
Linux, with any C library.
case MacOs
case MacOs
Apple's macOS.
case FreeBsd
case FreeBsd
FreeBSD.
const current
static current: OperatingSystem = targetOperatingSystem()
The operating system this program is compiled for: the --target of the build, the host by default, and in the VM
the machine the VM runs on. A compile-time constant, so a match on it keeps one arm.
Examples
print(OperatingSystem.current.isPosix() || true)
fn isPosix
fn isPosix(): Bool
Whether the system follows POSIX, which is every case but Windows. The question for a branch that should include
a system that is added later.
Examples
print OperatingSystem.Linux.isPosix()
fn show
fn show(): String
The name as its vendor writes it: Windows, Linux, macOS, FreeBSD.
Examples
print OperatingSystem.MacOs
type Architecture
type Architecture with Show, Equals, Hash
The processor architectures the toolchain builds for.
Examples
fn wordName(): String {
match Architecture.current {
.X64 => "x86-64"
.Arm64 => "arm64"
}
}
case X64
case X64
x86-64, also called AMD64.
case Arm64
case Arm64
64-bit Arm, also called AArch64.
const current
static current: Architecture = targetArchitecture()
The architecture this program is compiled for. A compile-time constant, like OperatingSystem.current.
fn show
fn show(): String
x86-64, arm64.
Examples
print Architecture.Arm64
type ByteOrder
type ByteOrder with Show, Equals, Hash
The order of the bytes of a number in memory, for a binary format that says "native order". Every target today is little endian; a decoder that reads a structure the operating system wrote says which order it assumes by asking.
case LittleEndian
case LittleEndian
The least significant byte first.
case BigEndian
case BigEndian
The most significant byte first.
const current
static current: ByteOrder = targetByteOrder()
The byte order of the target. A compile-time constant, like OperatingSystem.current.