data.optional


template <typename T>
struct optional §

Fields

  • bool is_valid §
    
  • T value §
    
template <typename T>
inline bool is_valid(optional<T> x) §
template <typename T>
inline optional<T> make_optional(bool is_valid, T value) §
inline auto just(auto value) §

Create optional with is_valid set to true.

template <typename T>
inline bool equal(optional<T> x, optional<T> y) §

Compare optionals for equality. Return true when both arguments are invalid or both arguments are valid and have equal values.

Examples
>>> equal({false, 0x0}, {false, 0x1});
true

>>> equal({true, 0x4}, {false, 0x4});
false

>>> equal({true, 0x4}, {true, 0x4});
true
template <typename T>
inline pair<bool, T> optional_to_pair(optional<T> x) §

Convert from optional<T> to pair<bool, T>.

template <typename T>
inline auto from_optional(T default_value, optional<T> x) §

Convert from optional<T> to T, using a default value if necessary.

Examples
>>> from_optional(0x7, {false, 0x2});
0x7

>>> from_optional(0x7, {true, 0x2});
0x2
template <typename T>
inline optional<T> join(optional<optional<T>> x) §

Remove one layer of optional, projecting T into the outer layer.

Examples
>>> join({false, {false, 0xFF}})
{false, 0xFF}

>>> join({false, {true, 0xFF}})
{false, 0xFF}

>>> join({true, {false, 0xFF}})
{false, 0xFF}

>>> join({true, {true, 0xFF}})
{true, 0xFF}
template <typename T>
inline bool equal_by((T, T) -> bool equality_fn, optional<T> x, optional<T> y) §

Equality comparison of optionals using the specified function.

Examples
>>> equal_by([](uint8 x, uint8 y){ return even(x) && even(y); }, {true, 0}, {false, 2})
false

>>> equal_by([](uint8 x, uint8 y){ return even(x) && even(y); }, {true, 0}, {true, 2})
true

>>> equal_by([](uint8 x, uint8 y){ return even(x) && even(y); }, {false, 0}, {false, 3})
true
inline auto lift_optional((auto) -> auto fn) §

Returns a function with the signature: (optional<A>)->optional<decltype(fn(A))>. The result is valid if and only if the input is valid. fn is called to compute the resulting value. If the result is invalid, then fn is not called, and the result value is undefined.

Arguments

  • (auto) -> auto fn
    

    Function used to convert values.

inline auto lift2_optional((auto, auto) -> auto fn) §

Returns a function with the signature: (optional<A>, optional<B>)->optional<decltype(fn(A, B))>. The result is valid if and only if both inputs are valid. fn is called to compute the resulting value. If the result is invalid, then fn is not called, and the result value is undefined.

Arguments

  • (auto, auto) -> auto fn
    

    Function used to convert values.