Skip to content

Optional

Spice
import "std/data/optional";

Optional<T> struct

Optionals in Spice are wrappers around values, that allow them to be empty. This can be used as better alternative to nil for empty values.

Constructors

ctor

Spice
public p Optional.ctor()

Initialize optional without a value

ctor

Spice
public p Optional.ctor(const T& data)

Initialize optional with an initial value

Parameters

Name Type Description
data const T& Initial data value

Methods

set

Spice
public inline p Optional.set(const T& data)

Set the value of the optional

Parameters

Name Type Description
data const T& New data value

get

Spice
public inline f<T&> Optional.get()

Get the value of the optional

Returns: T& — Data value

orElse

Spice
public inline f<T&> Optional.orElse(T& defaultValue)

Get the value of the optional. If the optional is not present, return the default value

Parameters

Name Type Description
defaultValue T&

Returns: T& — Data value or default value

orElseGet

Spice
public inline f<T&> Optional.orElseGet(f<T&>() getFct)

Get the value of the optional. If the optional is not present, return the value of getFct

Parameters

Name Type Description
getFct f<T&>()

Returns: T& — Data value or value of the function

clear

Spice
public inline p Optional.clear()

Clears the value of the optional

isPresent

Spice
public inline f<bool> Optional.isPresent()

Checks if a value is present at the moment

Returns: bool — Present or not

Operators

operator==

Spice
public f<bool> operator==<T>(const Optional<T>& lhs, const Optional<T>& rhs)

Check if two optionals are equal, i.e. they have the same presence and equal payloads

Parameters

Name Type Description
lhs const Optional<T>&
rhs const Optional<T>&

Returns: bool — Equal or not equal

operator!=

Spice
public f<bool> operator!=<T>(const Optional<T>& lhs, const Optional<T>& rhs)

Check if two optionals are not equal

Parameters

Name Type Description
lhs const Optional<T>&
rhs const Optional<T>&

Returns: bool — Not equal or equal