Skip to content

Conditional Code

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Conditional imports and declarations

A when condition decides whether a dependency or declaration participates in a module. It is evaluated against the environment available before that module loads. Code defined inside the module cannot supply the condition.

A false import condition skips lookup entirely, which makes platform-specific dependencies practical even when their files are absent.

import deliberately_absent when false
echo "portable branch loaded"
portable branch loaded

Bracketed exclusions can guard a function or a declaration block:

[when true]
{
    let $label = "included"
    fn selected(): String { $label }
}
[when false]
fn selected_elsewhere(): String { "excluded" }
echo (selected)
included

An included block contributes declarations to the surrounding scope; it does not create a new runtime scope. Excluded declarations are not evaluated.

A condition that cannot be evaluated is an error, rather than a false condition. For example, misspelling $os should not silently remove an implementation. Use conditions such as $os == "linux" or $os == "macos" for platform selection.

This is declaration selection, not textual substitution. The source still needs to be syntactically valid. Ordinary if handles runtime decisions after the module has loaded.


Related: Modules · Control flow · Environment

Clone this wiki locally