API Reference › Builtin functions › assert
function

assert

assert(cond: bool, msg: string)

When cond is false, prints file:line: assertion failed: <msg> to standard error and aborts (exit code 1); when it is true, execution continues. It is the guarded form of panic — for the programmer-bug cases the language should catch loudly rather than let corrupt the world: an index that must be in range, a value that must be non-negative, a state that must hold before a step. The location is baked in at compile time, so a failure points at the exact assertion. Assertions document and enforce the invariants a system relies on; keep them for "this must be true" checks, not for recoverable runtime conditions.

Parameters

condthe invariant that must hold (a bool)
msga message describing the invariant (a string)

Example

program Demo {
  function withdraw(balance: int, amount: int) -> int {
    assert(amount >= 0, "amount must be non-negative")
    assert(amount <= balance, "cannot overdraw")
    return balance - amount
  }
  entry {
    print(withdraw(100, 30))   # 70
    print(withdraw(100, 250))  # aborts: Demo.ludic:4: assertion failed: cannot overdraw
  }
}
← All symbols