Skip to content

Conditions & Match

Turn inputs into explicit decisions.

if chooses a branch from a Boolean condition. An if ... then ... else ... expression needs both results. match selects the first matching arm in source order.

Complete example

Save the example as control-flow.aal

control-flow.aal
agent Tutorial is Agent
  public
    ability run(score: Int) -> String effects()
      require score >= 0, "Score must be nonnegative"
      grade = if score >= 6 then "ready" else "revise"
      return match grade
        "ready" -> "Publish"
        _ -> "Review again"
Download .aal

Syntax explained

  1. Use ==, !=, <, <=, >, >=, in and not in. Comparisons may chain: 40 <= area < 100. Combine conditions with and or or.

  2. require condition, "message" fails with E_REQUIREMENT when false. Place it before the operation it protects.

  3. match supports literals, nullary enums, None and bindings. Put the _ catch-all last. Destructuring patterns are not supported.

Build and run

Terminal
cargo run --locked --bin aalc -- check control-flow.aal

cargo run --locked --bin aalc -- build control-flow.aal -o control-flow.aalc

cargo run --locked --bin aalc -- run control-flow.aalc \
  --agent Tutorial --ability run \
  --args '{"score":7}' \
  --state-dir .aal-tutorial/control-flow --invocation control-flow-1

Run with a fresh state directory. The returned value is shown below; the CLI also prints execution information.

Expected result
"Publish"

Try it yourself

Try score 3 to get Review again, then -1 to trigger require before grade is computed. Use a new invocation ID for each request.

Language source and specification