Modules · Data · Collection · Optional

Collection.Optional

An optional value — either present, carrying a value, or absent. Because it is a real generic union rather than a nullable flag, it nests cleanly and must be checked before its value can be used. Construct one with Collection.Optional.present or Collection.Optional.absent.

Contents

Types

Functions

Types

Collection.Optional

A value that is either present, carrying a value, or absent. Because it is a real generic union rather than a nullable flag, it nests cleanly: a Collection.Optional(Collection.Optional(Text)) can tell absent apart from present-but-empty. Construct one with Collection.Optional.present or Collection.Optional.absent.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Options

OptionTypeDescription
presentValueTypeThe present case; carries a value.
absentThe absent case; carries nothing.

Definition

TYPE Collection.Optional(ValueType)
  OPTION ValueType present
  OPTION absent
A Collection.Optional(ValueType) is either present with a value or absent.

Example

LET Collection.Optional(Text) name = Collection.Optional.present "Ada"
Constructs a present optional holding "Ada"; Collection.Optional.absent makes the empty case.

Functions

Collection.Optional.Default

The contained value, or a fallback when the optional is absent.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to read.
defaultValueValueTypeReturned when the optional is absent.

Returns

Returns ValueType — the contained value when present, otherwise defaultValue.

Example

LET Text shown = Collection.Optional.Default maybeName "guest"
Unwraps the optional, falling back to "guest" when it is absent.

Collection.Optional.If

Runs a callback with the contained value when an optional is present, and does nothing when it is absent.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to test.

Holes

NameReturnsDescription
callbacknothingRun once with the unwrapped value when the optional is present; never run when it is absent.
Inputs
NameTypeDescription
valueValueTypeThe value the optional holds, unwrapped.

Example

LET Collection.Optional(Text) maybeName = Collection.Optional.present "Ada"

// Run the callback only when the optional is present
Collection.Optional.If maybeName
  INPUT Text name
  Ui.Content.Text name
Constructs an optional holding "Ada"; Collection.Optional.If then runs the callback with the unwrapped value bound to name. When the optional is absent, the callback never runs.

Collection.Optional.IfNot

Runs a callback when an optional is absent, and does nothing when it is present.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to test.

Holes

NameReturnsDescription
callbacknothingRun once when the optional is absent; never run when it is present. It receives no inputs.

Example

Collection.Optional.IfNot maybeName
  Ui.Content.Text "No name yet"
Shows the placeholder text only while maybeName is absent.

Collection.Optional.IsAbsent

Yields yes when the optional carries no value.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to test.

Returns

Returns Logic.Maybe — yes when absent, otherwise no — the negation of IsPresent.

Example

LET Logic.Maybe missing = Collection.Optional.IsAbsent maybeName
The opposite of IsPresent.

Collection.Optional.IsPresent

Yields yes when the optional carries a value.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to test.

Returns

Returns Logic.Maybe — yes when present, otherwise no.

Example

LET Logic.Maybe hasName = Collection.Optional.IsPresent maybeName
A reactive test you can feed to Logic.If and friends.

Collection.Optional.Map

A new optional holding the result of a block, or absent when the input is absent.

Generics

NameDescription
OptionalTypeThe value type of the input optional.
ResultTypeThe type the block produces, and the value type of the result.

Parameters

NameTypeDescription
optionalValueCollection.Optional(OptionalType)The optional to transform.

Holes

NameReturnsDescription
mapFnResultTypeRun only when the optional is present, with its unwrapped value; its result becomes the new present value.
Inputs
NameTypeDescription
valueOptionalTypeThe unwrapped value, when present.

Returns

Returns Collection.Optional(ResultType) — present with the mapped result, or absent when the input was absent.

Example

LET Collection.Optional(Maths.Integer) length = Collection.Optional.Map maybeName
  INPUT Text name
  Text.Length name
Maps the value through when present, staying absent otherwise — no unwrapping needed.

Collection.Optional.Or

The first optional if present, otherwise the second.

Generics

NameDescription
ValueTypeThe type of value the optionals may hold.

Parameters

NameTypeDescription
optionalACollection.Optional(ValueType)The preferred optional.
optionalBCollection.Optional(ValueType)The fallback optional.

Returns

Returns Collection.Optional(ValueType)optionalA when present, otherwise optionalB.

Example

LET Collection.Optional(Text) chosen = Collection.Optional.Or fromUser fromDefaults
Prefers the user's value, falling through to defaults; the result is still an optional.

Collection.Optional.Select

Picks one of two values by whether the optional is present.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.
ResultTypeThe type of the two values chosen between, and of the result.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to test.
presentValueResultTypeReturned when the optional is present.
absentValueResultTypeReturned when the optional is absent.

Returns

Returns ResultTypepresentValue or absentValue, matching the optional.

Example

LET Text status = Collection.Optional.Select maybeName "signed in" "signed out"
Chooses purely on presence; unlike Default, the two results need not be the value's own type.

Collection.Optional.ToArray

The optional as an array: one element when present, empty when absent.

Generics

NameDescription
ValueTypeThe type of value the optional may hold.

Parameters

NameTypeDescription
optionalValueCollection.Optional(ValueType)The optional to convert.

Returns

Returns Collection.Array(ValueType) — a one-element array when present, an empty array when absent.

Example

LET Collection.Array(Text) names = Collection.Optional.ToArray maybeName
Bridges an optional into array operations like Each and Map.

Collection.Optional.Zero

An absent optional of the value type.

Generics

NameDescription
ValueTypeThe value type of the optional.

Returns

Returns Collection.Optional(ValueType) — an absent optional.

Example

STATE Collection.Optional(Text) maybeName = Collection.Optional.Zero
The empty starting point — the same as Collection.Optional.absent.