Skip to content

33 Nullable values ​

A ? on a type permits null; the type without ? does not.

norm
String? label(Boolean present) {
  if present {
    return "ready"
  }
  return null
}

main() {
  String? present = label(present: true)
  String? missing = label(present: false)
  printLine(present == null)
  printLine(missing == null)
}

Expected output:

text
false
true

label can return a string or absence, so its result is String?. Both bindings retain that nullable type even when one call currently returns a string; use a check or a fallback before requiring a non-null string.

Try it: Change the false branch to a string and compare the two null checks.

Precise rules: Reference.

Norm 0.25