Skip to main content

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 unionsMode = \dev` | `prod`` keeps configuration modes explicit and finite.
  • @constraint — the port binding fails fast if the computed value ever falls outside the valid port range.
  • withbase_config with { ... } expresses overrides without mutating the original object.
  • Type annotationsconfig: AppConfig = ... checks the final composed value against the named output type.

Run it

rank examples/hello-config