Conditionals Cookbook
This subsection is the canonical v0.40.x guide for Nitpick's conditional family:
the if / else if / else statement chain and the is (cond) : a : b inline
ternary. It supersedes the older, scattered conditional notes in
guide/control_flow/ and the if/ternary fragments embedded in
guide/basics/, where those pages are shorter or stale.
The v0.40.x cycle locked the shipped behavior of every conditional surface:
- the
ifstatement, theelse ifchain, and the trailingelse - strict
boolconditions (no truthiness; integers/pointers/Resultare not conditions on their own) - the
is (cond) : true_value : false_valueinline ternary as the only value-producing conditional besides thepickexpression - short-circuit evaluation: exactly one
isarm is ever evaluated - correct interaction with the unused-variable and dead-code warnings (a
variable used only inside an
isarm or a singleifbranch is used; code after a branch thatexits is dead) - borrow checking through every
isarm and everyifbranch - the diagnostics
ARIA-IF-001,ARIA-IF-002,ARIA-IF-003, andARIA-IS-001
Chapters
- Basics — the conditional mental model, statement vs expression,
and when to reach for
ifvsisvspick. - if / else if / else — the statement chain, block scope, and the order of arm evaluation.
- Conditions and
bool— strict-boolconditions, why there is no truthiness, andraw fn()forResult<bool>. - The
isternary —is (cond) : a : b, its type rules, and common patterns. - Short-circuit — exactly-one-arm evaluation in
is, and how it parallelsif. - Nested conditionals — nesting
ifandis, and how to keep chains readable. isvspickvswhen— choosing the right construct for a value, a multi-way branch, or a loop-completion hook.- Diagnostics —
ARIA-IF-001/002/003andARIA-IS-001, what triggers them, and how to fix the underlying mistake.
Quick mental model
- Reach for
if (c) { ... } else { ... }to do something conditionally. - Reach for
is (c) : a : bto compute a value conditionally. - Reach for a
pickexpression withgivefor a multi-way value. - Conditions are strictly
bool— there is no truthiness. A function returningResult<bool>must be unwrapped withraw fn()before it is a condition. ifis a statement: it never produces a value. Writingint32:x = if (c) { ... }is rejected withARIA-IF-003, which points you atisandpick.- An
isternary evaluates its condition, then reduces exactly one arm; the other arm is discarded unevaluated. - A variable used only inside an
isarm — or only inside oneifbranch — is a genuine use, never a false[unused-variable].
Validation snapshot (v0.40.x)
- Compiler non-K CTest: 201/201 (adds
bug_tests_v0401–v0406). - K core tests: 235/235 (adds 229–235 for the
isternary). - K proofs: unchanged.
- v0.40.x conditional bug regressions: bug951–bug990.
- Conditional diagnostics covered by this cycle:
ARIA-IF-001,ARIA-IF-002,ARIA-IF-003,ARIA-IS-001. - One compiler bug fixed mid-cycle: a mixed-width PHI-node verification failure
when an
isternary's two arms had different integer widths (e.g.int16andint32); the narrower arm is now sign-extended to the common type before the PHI is built.