Acton
Acton standard libraryTlb

maybe

maybe.tolk standard library file

Represents an optional value (TL-B Maybe) that may or may not be present. This is similar to the Option type in other programming languages.

Definitions

TlbMaybe

type TlbMaybe<X> = TlbNone | TlbJust<X>
Source code

TlbNone

struct (0b0) TlbNone

Represents the absence of a value.

Source code

TlbJust

struct (0b1) TlbJust<X> {
    /// The contained value.
    value: X
}

Represents a present value of type X.

Source code

TlbMaybe<X>.none

fun TlbMaybe<X>.none(): TlbMaybe<X>

Creates a TL-B Maybe instance with no value (TlbNone).

Example:

val empty = TlbMaybe<slice>.none();
Source code

TlbMaybe<X>.just

fun TlbMaybe<X>.just(value: X): TlbMaybe<X>

Creates a TL-B Maybe instance with the given value.

Example:

val present = TlbMaybe.just(42);
Source code

TlbMaybe<X>.unwrap

fun TlbMaybe<X>.unwrap(self): X

Unwraps the Maybe, returning the contained value if present, or throws an exception if TlbNone.

This is unsafe. Use it only when you are certain the value is present. For safe access, use unwrapOr instead.

Throws

  • Throws exit code 7 when called on TlbNone.

Example:

val maybeValue = TlbMaybe.just(42);
val value = maybeValue.unwrap(); // Returns 42
Source code

TlbMaybe<X>.unwrapOr

fun TlbMaybe<X>.unwrapOr<U>(self, default: U): X | U

Unwraps the Maybe, returning the contained value if present, or the provided default value if TlbNone.

This is the safe way to access optional values.

Example:

val maybeValue = TlbMaybe<string>.none();
val name = maybeValue.unwrapOr("default"); // Returns "default"
Source code

Last updated on

On this page