result

Generated by tinox doc

tinox.core.result, bundled with the compiler and also available as a standalone tinox-central package.

Ok and err

import tinox.core.result
class Main
{
    fnc divide(a: Int64, b: Int64) -> Result<Int64>
    {
        if (b == 0)
        {
            return Result<Int64>::err("division by zero")
        }
        return Result<Int64>::ok(a / b)
    }

    fnc main() -> Int32
    {
        const good: Result<Int64> = Main::divide(10, 2)
        if (good.isOk())
        {
            println("10 / 2 = " + good.unwrap().toString())
        }

        const bad: Result<Int64> = Main::divide(10, 0)
        if (bad.isErr())
        {
            println("error: " + bad.getError())
        }
        println("fallback: " + bad.unwrapOr(-1).toString())
        return 0
    }
}

class Result<T>

* Represents either a success value of type T or an error string. * Uses a List<T> internally to avoid requiring a zero value for T.

Fields

valuesList<T>
errorString

Methods

fn ok(value: T) → Result<T>
fn err(error: String) → Result<T>
fn isOk() → Bool
fn isErr() → Bool
fn unwrap() → T
fn unwrapOr(defaultValue: T) → T
fn getError() → String
fn map(transform: fnc(T) -> U) → Result<U>
fn andThen(transform: fnc(T) -> Result<U>) → Result<U>
fn mapErr(transform: fnc(String) -> String) → Result<T>