Optional
Typed presence or absence, explicit checked payload access, managed identity and ownership, and time and space complexity.
ImplementedOptional<T> is an import free typed value that explicitly contains a value of T or contains no value. It represents missing scalar, object, and native values without assigning a sentinel number or overloading an empty String.
Expected type construction below requires Aner 0.0.4 or newer. Explicitly specialized factories remain available with their existing semantics; older published binaries may require the type argument on Optional.none.
Construction and checked access
let missing: Optional<Float64> = Optional.none()
let measured = Optional.some(3.5)
print(missing.is_none()) // true
print(measured.is_some()) // true
print(missing.unwrap_or(default: 0.0)) // 0.0
if measured.is_some() {
print(measured.unwrap()) // 3.5
}Optional.some<T>(value: T) constructs a present value. T can be inferred from the supplied concrete value. Optional<T>(value: T) is an equivalent constructor. Optional.none<T>() explicitly names the absent payload type. With an expected Optional<T> from an annotated binding, assignment, declared return, typed user function argument, declared class constructor field or wrapper unwrap_or fallback, Optional.none() and Optional.some(value) can use that known T. Numeric payloads follow Number and checked exact Float64 promotion: Optional<Number> preserves any supported integer type, Float32, or Float64 payload, while Optional<Float64> may accept an exactly representable integer. Other payloads retain their declared type contracts.
Nested factories receive the known payload type too:
let result: Result<Optional<Int64>, String> = Result.ok(Optional.none())
fn missing_value() -> Optional<Float64> { return Optional.none() }
print(result.unwrap().is_none())
print(missing_value().is_none())This prints true twice. An unannotated let missing = Optional.none() still fails because neither absence nor its context supplies T. Explicit factory arguments remain valid and must agree with the expected type.
A payload may have a scalar, native, user class, collection, or nested generic type. The wrapper retains that declared concrete type. Scalar unit values are supported payloads after import aner.units: let absent: Optional<Quantity<m>> = Optional.none() and Optional.some(2.0@m) retain the exact declared unit. A fallback must match that exact payload type; an explicit .to("m") converts another compatible unit before wrapping or access. Wrapping a scalar preserves its immutable value semantics and does not add measurement evidence. A present nullable payload and an absent Optional remain distinct concepts. Optional is separate from existing nullable class references and dataset column missingness; those existing contracts retain their own accessors and checks.
Unit bearing arrays can also be retained as exact native payload types, such as Optional<Array<Quantity<m>>>. A wrapper retains the mutable array reference under the existing alias/copy contract; it does not turn its buffer into an immutable scalar or convert the element unit.
| Method | Result | Contract |
|---|---|---|
is_some() | Bool | Whether the wrapper contains a payload. |
is_none() | Bool | Whether the wrapper is absent. |
unwrap() | T | Return the present payload; absence produces a located runtime error. |
unwrap_or(default: T) | T | Return the payload when present, otherwise the supplied same type default. |
Access is explicit and checked. Testing is_some() does not automatically change the wrapper's static type or remove the need for a payload accessor. unwrap_or is an ordinary call: its default expression is evaluated even when the Optional is present. Optional has no postfix propagation operator, pattern matching or lazy default callback. The Result ? operator is specific to explicit Result returning functions and does not turn Optional absence into an error.
Ownership and type inspection
An Optional is an immutable managed wrapper. Copies share the wrapper's identity. A reference payload retains its existing ownership: for example, a wrapped List remains a mutable shared List, and wrapping it does not deep freeze or copy the collection. The execution heap traces wrapped references, including reachable cycles, using the same object and slot limits as other managed values.
Equality compares wrapper identity, without invoking payload equality or user code. Separately constructed present wrappers are therefore distinct even when their payloads are equal. Compare checked payloads explicitly when payload value equality is required. Wrappers are not scalar Dictionary keys or Set members.
type(value) reports the concrete specialization, such as Optional<Float64>. cast<Optional<Float64>>(value) checks that specialization when recovering a value from Any. dtype is unsupported for Optional: a payload type is not a homogeneous element storage datatype.
Errors and limits
Unchecked access to an absent alternative or a wrapper operation limit failure produces a located runtime error R3404. Wrong static argument types or arities produce E1002. Generic wrapper nesting is limited to 128 and formatted type names to 16 KiB; exceeding these static bounds produces E1007. Shared managed object and value slot limits also apply. A runtime failure retains the normal program or persistent session error contract.
Time and space complexity
The wrapper retains O(1) active state metadata and zero or one payload slot, plus its generic type descriptor. Let g describe the materialized generic type descriptor or name length. Construction and descriptor copying can add O(g) time and space. Presence tests and identity comparison use O(1) active state work plus O(g) receiver descriptor validation at the heap boundary; access adds that validation and the returned payload copy cost. Payload evaluation and copying follow the payload's existing semantics; an owned String can add O(b) byte time and storage, while reference payloads share their existing object. Managed garbage collection adds its normal reachable object and slot traversal cost. Unwrapping does not materialize an entire collection.
Run the complete Optional example, and see Result for a typed recoverable error outcome.