Skip to content

Result Rt

Spice
import "std/runtime/result_rt";

Result<T> struct

Result in Spice are wrappers around values, that allow to either provide a value or an error object. This is useful for functions that return a value, but also can fail. The error can then be dealt with on the caller side e.g. using the panic builtin.

Constructors

ctor

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

Construct a successful result holding the given data

Parameters

Name Type Description
data const T& Data value to wrap

ctor

Spice
public p Result.ctor(const Error& error)

Construct a failed result holding the given error

Parameters

Name Type Description
error const Error& Error to wrap

Methods

unwrap

Spice
public inline f<T&> Result.unwrap()

Returns the stored data object. If an error is present, this function will panic.

The returned reference is borrowed - the result stays the owner of the data and destructs it when it goes out of scope. To take ownership of a heap payload, move it out of the result: heap byte* p = result.unwrapAndMove();

Returns: T&

unwrapOr

Spice
public inline f<T&> Result.unwrapOr(const T& alternativeValue)

If no error is present, return the stored data object, otherwise the given value.

Parameters

Name Type Description
alternativeValue const T& Alternative data value for the error case

Returns: T&

unwrapOrElse

Spice
public inline f<T&> Result.unwrapOrElse(f<T&>(const Error&) callback)

If no error is present, return the stored data object, otherwise invoke the given callback.

Parameters

Name Type Description
callback f<T&>(const Error&) Callback which is invoked in case of an error

Returns: T&

unwrapAndMove

Spice
public inline f<T> Result.unwrapAndMove()

Move the stored data object out of the result object If an error is present, this function will panic.

Returns: T

getErr

Spice
public inline f<Error&> Result.getErr()

Return the enclosed error object.

Returns: Error&

isOk

Spice
public inline f<bool> Result.isOk()

Checks if the result contains any data.

Returns: bool

isErr

Spice
public inline f<bool> Result.isErr()

Checks if the result contains an error.

Returns: bool

dumpTrace

Spice
public inline p Result.dumpTrace()

Prints the recorded error return trace to stderr. Forwards to the enclosed error's dumpTrace(), see there.

Functions

ok

Spice
public inline f<Result<T>> ok<T>(const T& data)

Returns a result object with a value and no error.

Parameters

Name Type Description
data const T&

Returns: Result<T>

err

Spice
public inline f<Result<T>> err<T>(const Error& error)

Returns a result object with an error and no value.

Parameters

Name Type Description
error const Error&

Returns: Result<T>

err

Spice
public inline f<Result<T>> err<T>(int code, string message)

Returns a result object with an error and no value.

Parameters

Name Type Description
code int
message string

Returns: Result<T>

err

Spice
public inline f<Result<T>> err<T>(string message)

Returns a result object with an error and no value.

Parameters

Name Type Description
message string

Returns: Result<T>