Skip to content

36 Null fallback ​

?? uses its right operand only when the left operand is null.

norm
String fallback() {
  printLine("computed")
  return "guest"
}

main() {
  String? present = "Norm"
  String? missing = null
  printLine(present ?? fallback())
  printLine(missing ?? fallback())
}

Expected output:

text
Norm
computed
guest

The first call prints Norm without invoking fallback(). The second prints computed before guest, proving the fallback function is evaluated only for absence.

Try it: Make present null too and count how often the fallback runs.

Precise rules: Reference.

Norm 0.25