Skip to content

sonolus.script.maybe

Nothing: Maybe[Any] module-attribute

The empty Maybe instance.

Maybe

Bases: TransientValue

A type that either has a value or is empty.

Maybe has special behavior when returned from a function: unlike records and arrays, it may be returned from multiple places in a function, provided that all but one return statement returns the literal Nothing.

Storing values of this type in a Record, Array, or Archetype is not supported.

Usage
def fn(a, b):
    if a:
        return Some(b)
    else:
        return Nothing

result = fn(..., ...)
if result.is_some:
    value = result.get()
    ...

is_nothing: bool property

Check if the value is empty.

is_some: bool property

Check if the value is present.

tuple: tuple[bool, T] property

Return whether the value is present and a copy of the contained value if present as a tuple.

If the value is not present, the tuple will contain False and a zero initialized value of the contained type.

flat_map(fn: Callable[[T], Maybe[R]]) -> Maybe[R]

Flat map the contained value to a new Maybe using the provided function.

If the value is not present, returns Nothing.

Parameters:

Name Type Description Default
fn Callable[[T], Maybe[R]]

A function that takes the contained value and returns a new Maybe.

required

Returns:

Type Description
Maybe[R]

A Maybe instance containing the result of the function if the value

Maybe[R]

is present, otherwise Nothing.

get() -> T

Get the value if present, otherwise raise an error.

map(fn: Callable[[T], R]) -> Maybe[R]

Map the contained value to a new value using the provided function.

If the value is not present, returns Nothing.

Parameters:

Name Type Description Default
fn Callable[[T], R]

A function that takes the contained value and returns a new value.

required

Returns:

Type Description
Maybe[R]

A Maybe instance containing the result of the function if the value

Maybe[R]

is present, otherwise Nothing.

or_default(default: T) -> T

Return a copy of the contained value if present, otherwise return a copy of the given default value.

Parameters:

Name Type Description Default
default T

The default value to return if the contained value is not present.

required

Returns:

Type Description
T

A copy of the contained value if present, otherwise a copy of the default value.

or_else(fn: Callable[[], T]) -> T

Return a copy of the contained value if present, otherwise return a copy of the result of the given function.

Parameters:

Name Type Description Default
fn Callable[[], T]

A function that returns a value to use if the contained value is not present.

required

Returns:

Type Description
T

A copy of the contained value if present, otherwise a copy of the result of calling the function.

Some(value: T) -> Maybe[T]

Create a Maybe instance with a value.

Parameters:

Name Type Description Default
value T

The contained value.

required

Returns:

Type Description
Maybe[T]

A Maybe instance that contains the provided value.