|
spudplate
Template scaffolding compiler for spudlang .spud files
|
Runs the body block when <condition> evaluates to true. The condition uses the same expression grammar as when. Nested if and repeat, ask, and any action statement may appear inside.
if and the per-statement when clause solve the same problem from opposite ends:
when gates a single statement.if gates a block of statements with one shared condition.If you find yourself writing the same when <cond> on three or more consecutive statements, if is usually clearer:
versus
Both are valid. The if form makes the relationship between the statements visible.
Spudlang has no else or else if. A multi-branch decision composes from nested if blocks or from per-statement when clauses with mutually-exclusive conditions.
For two-branch decisions, two if blocks with negated conditions work, but a when on each statement is often more readable.
An if block does not itself accept a when clause: the if is the gate. To express "do this block only when `a and b`", put the compound condition on the if:
Inner statements may still carry their own when, which combines with the outer if:
An ask lexically inside an if body that has no inner when clause does not need a default. The if is the gate.
If use_tests is false, the inner ask never prompts. The variable test_runner is not bound in that case.
An ask inside if that also carries its own when clause still needs a default, because the inner gate can be false while the outer if is true.
An if body introduces a new scope. let and as declared inside the block are local to it and are not visible after end. The no-shadowing rule applies.
A path alias bound by as inside an if is conditionally scoped under the if's condition.
if or when to gate inside the loop.let, as, and ask inside an if.<condition>.