Skip to main content
Module

x/eitherway/mod.ts>Some

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
namespace Some
import { Some } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

Functions

Use this to signal some kind of success irrespective of the wrapped type as alternative to Some<void>

type alias Some
import { type Some } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

Some

Some<T> represents the encapsulation of a value of type <T> An instance of Some can only be constructed from non-nullish values, so the construction explicitely asserts that the value is not nullish

Use Option to produce a value of type Option<T> if T can be nullish.

Be aware that this is not only a compile time check, but also enforced at runtime.

Some<T> is a thin wrapper around <T>, in addition to the API one would expect, it implements the iterator protocol and delegates to the underlying implementations of <T> when:

  • used as an IterableIterator (returns <T> if not implemented)
  • explicitely or implicitely coerced
  • encoded as JSON via JSON.stringify()

Please checkout None for the opposite case.

Examples

Example 1

import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";

const str = "thing";
const some = Some(str);
const rec = { some };
const arr = [ ...some ]; //`String.prototype[@@iterator]()` -> UTF-8 codepoints

assert(some instanceof Some === true);
assert(some.isSome() === true);
assert(some.isNone() === false);
assert(some.unwrap() === str);
assert(String(some) === str);
assert(arr.join("") === str);
assert(JSON.stringify(some) === JSON.stringify({ some: "thing" }));
definition: _Some<T>
function Some
import { Some } from "https://deno.land/x/eitherway@0.10.0/mod.ts";