Repository navigation
Conditional Code
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
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use