Skip to main content
Version: 1.0.0

Optional values

Optional of T explicitly distinguishes a present value from an absent one. A missing address, an empty string and a populated address are different business states.

Construct and read a value​

contacts.tb
module examples.contacts

function FirstContact
takes contacts as List of Optional of Text
gives Optional of Text
do
for contact in contacts
if present contact as address
if address != ""
give some(address)
give none(Text)

some(address) constructs a present value. none(Text) constructs an absence with an explicit type. if present exposes address only inside the presence branch. It does not allow access to an absent value.

The first executed give exits the function. If the list contains no non-empty address, the function returns absence, not invented text.

JSON representation​

Touba valueJSON argument or result
none(Text){"present":false}
some(""){"present":true,"value":""}
some("a@example.test"){"present":true,"value":"a@example.test"}

Neither null, a bare string nor an omitted required field replaces this envelope. The runtime validates presence and the contained value's type. A field declared as Optional of T therefore keeps its place in the JSON record; its business absence is represented by present: false.

Uses and limits​

Optionals compose with lists, records and enumerations. Prefer a presence check to a sentinel such as -1 or "unknown" when the domain gives that value no independent meaning.

There is no implicit unwrapping, automatic conversion to Text or general equality of optional envelopes. Check presence, then process the value inside its branch.

For a complete walkthrough, follow Classify typed tickets.