Compile-Time Conditionals (v0.2.1)

Target-dependent branching using @if, @elif, and @else.

Latest

Overview

Compile-time conditionals choose code before the program is generated. The inactive branches are not emitted.

They are useful for platform-specific imports, constants, and declarations.

Use @if with target builtins such as isLinux or isWindows when a program needs different code for different targets.

Primary source: syntax/statements/compiletime.md

Syntax Signatures

@if(isLinux()) const PLATFORM: u32 = 1;
@elif(isWindows()) const PLATFORM: u32 = 2;
@else const PLATFORM: u32 = 0;

Behavior and Use

Only the selected branch is kept. Each branch should still be valid for the target where it can be selected.

If many files need the same target decision, put that decision in one small module instead of repeating it everywhere.

Best Practices

Selection Time

Compile-time conditionals are evaluated before code generation.

Predicates

Predicates usually come from target and compiler builtins.

Example

@if(isLinux()) const PLATFORM: u32 = 2;
@elif(isWindows()) const PLATFORM: u32 = 1;
@else const PLATFORM: u32 = 3;

fn currentPlatform() u32 @public {
    return PLATFORM;
}

Back to language reference