Hello Config
A compact Rank program that still shows validation and composition, not just object literals.
Source
examples/hello-config/main.rank
/// A small but realistic configuration object demonstrating
/// type aliases, constraints, and non-destructive overrides with `with`.
Mode = `dev` | `prod`
AppConfig = Object {
name: string,
host: string,
port: number,
debug: bool,
}
mode: Mode = `dev`
@constraint(cond: |self| self >= 1 && self <= 65535)
port = mode == `prod` ? 443 : 8080
base_config = {
name: `my-app`,
host: `localhost`,
port: 80,
debug: false,
}
config: AppConfig = base_config with {
port: port,
debug: mode == `dev`,
}
pub main = || config
Output
{
"name": "my-app",
"host": "localhost",
"port": 8080,
"debug": true
}
Key concepts
- Literal unions —
Mode = \dev` | `prod`` keeps configuration modes explicit and finite. @constraint— theportbinding fails fast if the computed value ever falls outside the valid port range.with—base_config with { ... }expresses overrides without mutating the original object.- Type annotations —
config: AppConfig = ...checks the final composed value against the named output type.
Run it
rank examples/hello-config