Skip to main content

Optionality

Nullity is a primary source of bugs in software. Being able to guarantee that a value will never be null makes it easier to write safe code without constantly having to take nullity into account.

An optional value can be either "nil" or a non-nil value. The type of an optional variable is represented by adding a question mark (?) to its end.

main.w
let monday: str = "doctor";
let tuesday: str? = nil;

// Set next to tuesday if there is a value otherwise use monday value
let next = tuesday ?? monday;

log(next);

Wing console output
# Run locally with wing console
wing it

doctor