Types and Optionals
The runtime provides the type names integer, float, string, boolean,
and NoneType, and the value bindings true, false, and none. These names
are identifiers, not keywords. Type descriptions
are not ordinary values.
| Control | Result |
|---|---|
optional(type) | Type description |
is_none(value) | Boolean |
unwrap(value) | Input value, unless absent |
or_else(value, fallback) | Input value or fallback value |
For compound types, see array and record.
optional
Evaluates its argument as a type description and returns a type accepting that
type or none. It does not wrap or convert values. Repeated construction of
the same optional type returns the same type; nested optionals normalize to one
optional, and optional(NoneType) is NoneType.
declare(answer, optional(integer), none)
set(answer, 42)
print(answer) / 42
set(answer, none)
Optional fields still require a value in record_of;
pass none explicitly. To build arrays accepting both values and absence, use
empty_array with an optional element type.
See optional type compatibility for assignment and overload rules. Passing a value instead of a type is an error.
is_none
Evaluates its argument once and returns whether the resulting value is none.
Accepts any runtime value, not just one read from an optional variable. A type
description or an expression with no value is an error.
print(is_none(none)) / true
print(is_none(0)) / false
It cannot test an uninitialized variable: reading that variable fails first.
unwrap
Evaluates its argument once. Returns any non-absent value unchanged; none
causes an error. It neither changes the variable’s declared type nor copies
an object.
declare(answer, optional(integer), 42)
print(unwrap(answer)) / 42
or_else
Evaluates the first argument once. Returns it if it is not none; otherwise,
evaluates the fallback and returns its value. The fallback is lazy and must
produce a runtime value if selected. It may return none too.
declare(answer, optional(string), none)
print(or_else(answer, "No answer")) / No answer
print(or_else("present", unwrap(none))) / present
There is no requirement that both branches have the same type. The receiving variable or Function checks the type of the result. An uninitialized first argument is an error, not a reason to select the fallback.