Skip to content

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 ​

ts
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 ​

sh
keika check comparisons-and-short-circuit.km
keika run comparisons-and-short-circuit.km

Expected output:

text
true true true true true false true true 1

Read the result ​

ExpressionResultContract
1 == 1trueGo-shaped equality is typed and non-coercive.
1 === 1trueTypeScript-shaped equality has the same semantics.
1 != 2, 1 !== 2true, trueBoth inequality spellings negate the same typed comparison.
"hello" < "planet"trueStrings use Go's byte-wise lexicographic order.
false && record(true)falseThe right operand is skipped.
`truerecord(false)`
true && record(true)trueThe 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.

Kinmokusei is a pre-1.0 project. Documentation describes implemented behavior.