Basics
Nitpick has two conditional surfaces, and the difference between them is the single most important thing to internalize:
ifis a statement. It chooses which code to run. It does not produce a value.isis an expression. It chooses which value to compute. It produces a value and can appear anywhere a value is expected.
func:classify = int32(int32:n) {
// `if` runs one branch or the other.
if (n < 0) {
println("negative");
} else {
println("non-negative");
};
// `is` computes a value.
int32:sign = is (n < 0) : -1 : 1;
pass(sign);
};
Statement vs expression
Because if is a statement, it can only appear where a statement is allowed β
at block level, as its own line. You cannot assign it, pass it as an argument,
or return it:
int32:x = if (c) { 1i32 } else { 2i32 }; // ERROR ARIA-IF-003
The compiler rejects this with ARIA-IF-003 and points you at the two
value-producing conditionals: the is ternary and the pick expression with
give. See Diagnostics.
The expression form is:
int32:x = is (c) : 1i32 : 2i32; // OK
When to reach for each
| You want to⦠| Use |
|---|---|
| run one of two blocks of code | if / else |
| run one of several blocks by value | pick (stmt) |
| compute one of two values | is ternary |
| compute one of several values | pick + give |
| run a loop and react to how it finished | when |
Conditions are always bool
Every condition β in if, in is, in while, in when β is strictly bool.
There is no truthiness: an integer, a pointer, or a bare Result is not a
condition. This is covered in Conditions and bool.
The shared shape
Both forms share the same "test, then choose" shape, and the K semantics gives them parallel rules:
if (true) B else _ => B is (true) : A : _ => A
if (false) _ else B => B is (false) : _ : B => B
The next chapters cover each form in detail.