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.

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

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>