result.unwrap

The value of a success, or a thrown KernelDiagnosticError carrying the failure’s first error.

GeometricSince 0.3.0

Usage

TypeScript
Result.unwrap<T>(r: Result<T>): T

Parameters

  • rResult<T>

    The result to read.

    Required

Returns

The success value; for a failure it does not return but throws.

Errors

Guarantees

  • on success it returns r.value (===)
  • on failure the thrown error is a KernelDiagnosticError whose diagnostic is Result.error(r) (e.diagnostic.code === Result.error(r).code)

Use instead

  • result.valueOr: a fallback value is acceptable and no exception is wanted

Example

TypeScript
Result.unwrap(Result.ok(3)) // → 3
let code; try { Result.unwrap(Result.fail('GEOMETRY_INPUT_INVALID', 'bad radius')) } catch (e) { code = e.diagnostic.code }
code // → 'GEOMETRY_INPUT_INVALID'

See also