std/result

Standard Library documentation

A simple Result object for explicit success and failure values.

Module

Name
std/result
Area
Standard Library
Source
modules/std/result.zzm

NAME

std/result - A simple Result object for explicit success and failure values.

SYNOPSIS

  from std/result import Result;

  let r := Result.ok(42);

  if ( r.is_ok() ) {
    say r.unwrap();
  }

IMPLEMENTATION SUPPORT

This module is supported by all implementations of ZuzuScript.

DESCRIPTION

Result is a small, subclassable object modelled on a simplified version of Rust's Result concept. It is useful when a function, task, or worker wants to return an explicit success or failure value without throwing an exception.

It is a normal ZuzuScript class. Workers do not require Result; any value supported by std/marshal may be returned.

EXPORTS

Classes

  • Result

    Container for either an ok value or an error value.

    • Result.ok(value)

      Parameters: value is any success value. Returns: Result. Creates an ok result wrapping value.

    • Result.err(error)

      Parameters: error is any error value. Returns: Result. Creates an error result wrapping error.

    • result.is_ok()

      Parameters: none. Returns: Boolean. Returns true when the result is an ok value.

    • result.is_err()

      Parameters: none. Returns: Boolean. Returns true when the result is an error value.

    • result.value()

      Parameters: none. Returns: value. Returns the stored ok value, or null for an error result.

    • result.error()

      Parameters: none. Returns: value. Returns the stored error value, or null for an ok result.

    • result.unwrap()

      Parameters: none. Returns: value. Returns the ok value, or throws if the result is an error.

    • result.unwrap_err()

      Parameters: none. Returns: value. Returns the error value, or throws if the result is ok.

COPYRIGHT AND LICENCE

std/result is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.