Compare values and observe short-circuit evaluation
Kinmokusei has TypeScript-shaped strict equality spellings, but it does not have JavaScript coercion. This recipe proves the four equality spellings and makes the evaluation boundary of && and || visible.
Source
import go fmt from "fmt";
let evaluations = 0;
function record(value: boolean): boolean {
evaluations++;
return value;
}
function main(): void {
const skippedAnd = false && record(true);
const skippedOr = true || record(false);
const evaluated = true && record(true);
fmt.Println(
1 == 1,
1 === 1,
1 != 2,
1 !== 2,
"hello" < "planet",
skippedAnd,
skippedOr,
evaluated,
evaluations,
);
}Run
keika check comparisons-and-short-circuit.km
keika run comparisons-and-short-circuit.kmExpected output:
true true true true true false true true 1Read the result
| Expression | Result | Contract |
|---|---|---|
1 == 1 | true | Go-shaped equality is typed and non-coercive. |
1 === 1 | true | TypeScript-shaped equality has the same semantics. |
1 != 2, 1 !== 2 | true, true | Both inequality spellings negate the same typed comparison. |
"hello" < "planet" | true | Strings use Go's byte-wise lexicographic order. |
false && record(true) | false | The right operand is skipped. |
| `true | record(false)` | |
true && record(true) | true | The right operand runs once. |
The final evaluations value is 1, not 3: only the third call is needed to determine its expression. Operands that do run are evaluated once from left to right.
Type boundaries
Equality requires compatible comparable values. It never turns a string into a number or a number into a boolean. Slices, maps, and functions are not generally comparable; arrays and structs are comparable only when every contained value is comparable.
Ordering requires two values of the same ordered type. Convert numeric widths explicitly before comparison, and use a concrete nullable or nil-capable value when comparing against null or nil.
See Operators for precedence and operand restrictions, and Expressions and evaluation for the broader once-only evaluation contract.