Reactive bindings and contracts

bind creates a variable that recomputes automatically when its dependencies change. where clauses add runtime enforcement to bindings and parameters.

Reactive bindings

bind total = price * qty tracks which variables are read when the expression is first evaluated. When any of those variables are reassigned, the binding recomputes.

scratch.xs

Works with strings and any expression:

scratch.xs

Cascading dependencies

A bind can depend on another bind. When the root dependency changes, all downstream bindings update in order.

scratch.xs

Contracts

Add a where clause after a type annotation to enforce a condition at runtime. The condition is checked when the binding is evaluated or the parameter is passed.

scratch.xs

Contracts on function parameters enforce preconditions at call sites:

scratch.xs

Contracts are gradual: omitting a where clause means no checking. Add them where the invariant matters and the cost of checking is justified.