Syntax · 2 of 6

Variables (let)

OxiScript has a single binding form: let name = expr. Every variable lives until the end of the procedure.

Basic binding

proc demo() {
    let x = 42
    let name = "Alice"
    let active = true
    return {x: x, name: name, active: active}
}

Binding to an expression

proc area(w, h) {
    let perimeter = 2 * (w + h)
    let surface   = w * h
    return {perimeter: perimeter, surface: surface}
}

Binding to a database call

proc balance(account_id) {
    let acc = find_one("accounts", {account_id: account_id})
    return acc.balance
}

Binding to another procedure's result

proc double_balance(account_id) {
    let bal = balance({account_id: account_id})
    return bal * 2
}

Reusing a binding

Re-binding with let works — the new binding shadows the old one for the rest of the procedure.

proc step() {
    let x = 1
    let x = x + 10     // now 11
    let x = x * 2      // now 22
    return x
}

Naming rules

  • Letters, digits, underscore. Must not start with a digit.
  • Case-sensitive: Namename.
  • Reserved words: proc, let, if, else, for, in, return, abort, true, false, null.
  • Convention: snake_case for variables and procedures.

Parameters are bindings too

Procedure parameters behave exactly like let bindings; you can read but not reassign them.

proc greet(name) {
    let message = "Hello, "
    return {greeting: message, who: name}
}

Order matters

You can only refer to a name after it's been bound.

proc bad() {
    return y     // ERROR — y is not bound yet
    let y = 1
}
proc good() {
    let y = 1
    return y
}

Bindings inside if blocks

Bindings inside an if are scoped to the procedure (no block-level scope), so they're visible after the block too.

proc fee(total) {
    if total > 100 {
        let discount = total * 0.1
        return {total: total, discount: discount}
    }
    return {total: total, discount: 0}
}
Tip: Bind anything you'll use more than once. let user = find_one(...) then refer to user.name, user.age, etc. — the database call only happens once.
Report Issue