Skip to content

Null, unassigned values, and fallback

Unassigned means a binding has never received a value. Null is an explicitly assigned absence. A nullable type displays ?, such as Number?.

Choose nullable when absence is a legitimate value. Non-nullable inputs require a usable value of the expected type. You cannot feed a nullable Number into arithmetic merely because it happens to contain a Number today; coalesce it to a compatible non-null result first.

Set Variable → Coalesce (??) takes a destination, primary, and fallback. It evaluates fallback only when primary is null or unassigned, and never assigns the primary. Other failures still propagate.

The expression equivalent is:

${save.bestScore ?? 0}

Declare bestScore first. An undeclared name is not simply an unassigned binding. Expression fallback branches must have compatible types.

Omitting a slot may invoke a declared default. Supplying a Null item does not select that default. A nullable optional method parameter may omit a default and receive null.

Assigning null consumes a Final binding’s first assignment. It is therefore incorrect to initialize a Final nullable field to null and expect to replace it later. Leave it unassigned until its intended initialization, or use a mutable binding.

Use Is Assigned/Variable Exists for assignment state and appropriate typed conditions for value checks. Do not use a missing target or out-of-range collection operation as a stand-in for a supported absence value.