Home / Docs / Standard library / Prelude

Result

Release v0.4.0Edition 0.1 and later

A kind of the prelude, in scope in every module. Edition 0.1 and later.

una
pub kind Result[T, E] = Ok(value: T) | Err(failure: E)

Either Ok(value), which holds the success value of T, or Err(failure), which holds the failure of E (spec/14 §1).

Variants

See also

is_ok

Example

src/shop.unauna
module shop

--- A quantity checked without failing: the outcome is a value.
checked_quantity(units: Int) -> Result[Int, Text] = if units > 0 then Ok(units) else Err("no units")

describe(units: Int) -> Text
=
  when checked_quantity(units)
    is Ok(n) -> "{n} units"
    is Err(failure) -> failure

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(describe(3))
  @console.print(describe(0))

una run . prints:

Output
3 units
no units

Specification

Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.