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>TlbNone
struct (0b0) TlbNoneRepresents the absence of a value.
Source codeTlbJust
struct (0b1) TlbJust<X> {
/// The contained value.
value: X
}Represents a present value of type X.
Source codeTlbMaybe<X>.none
fun TlbMaybe<X>.none(): TlbMaybe<X>Creates a TL-B Maybe instance with no value (TlbNone).
Example:
val empty = TlbMaybe<slice>.none();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);TlbMaybe<X>.unwrap
fun TlbMaybe<X>.unwrap(self): XUnwraps 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
7when called onTlbNone.
Example:
val maybeValue = TlbMaybe.just(42);
val value = maybeValue.unwrap(); // Returns 42TlbMaybe<X>.unwrapOr
fun TlbMaybe<X>.unwrapOr<U>(self, default: U): X | UUnwraps 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"Last updated on