Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

null-union

Flags a type written as a union with Null, like String or Null, that can use the ? shorthand instead.

Why

String? and String or Null mean the same thing. The shorthand is shorter, and reading ? as “may be null” is quicker than spotting Null at the end of a union.

What it flags

A union of exactly two types where one of them is Null, wherever a type can be written: parameters, return types, variables, typedefs, casts, generic parameters, dictionary and tuple types, Method(…) types and interfaces.

Written asBecomes
String or NullString?
Null or StringString?
String | NullString?
String or Toybox.Lang.NullString?
String? or NullString?
Array<String> or NullArray<String>?
[Number, String] or Null[Number, String]?
{ :flag as Boolean } or Null{ :flag as Boolean }?
(Method() as Boolean) or Null(Method() as Boolean)?
Method() as Boolean or NullMethod() as Boolean?

The last row is a nullable return type, not a nullable Method(…), since or Null binds to the return type. The fix keeps that meaning.

What it leaves alone

  • Unions of more than two types, like String or Number or Null. ? can only make a single type nullable.
  • Unions without Null, like String or Number.
  • A plain Null, like function f() as Null.

Example

// Before
var label as String or Null;

function onUpdate(dc as Dc or Null) as Array<Number or Null> {
    return [];
}

// After
var label as String?;

function onUpdate(dc as Dc?) as Array<Number?> {
    return [];
}