Nanyx

result

Composing success/error values

The result module provides helpers for values represented as #some(a) | #error(e).

map

Result.map: (#some(a) | #error(e), (a -> b)) -> #some(b) | #error(e)

Transforms successful values and leaves errors unchanged.

#some(21) \Result.map { * 2 }
-- #some(42)

mapError

Result.mapError: (#some(a) | #error(e1), (e1 -> e2)) -> #some(a) | #error(e2)

Transforms error values.

#error("bad") \Result.mapError { e -> "ERR: {e}" }
-- #error("ERR: bad")

bind

Result.bind: (#some(a) | #error(e), (a -> (#some(b) | #error(f)))) -> #some(b) | #error(e | f)

Chains result-producing operations. The error cases of both steps are preserved in the result, so the caller can still distinguish them.

readLine()
  \Result.bind(parseInt)
-- #some(int) | #error(IoError | #invalidInt)

defaultValue

Result.defaultValue: ((#some(a) | #error(e)), a) -> a

Returns the success value or fallback.

Result.defaultValue(#error("x"), 0)
-- 0

isOk

Result.isOk: (#some(a) | #error(e)) -> bool
Result.isOk(#some(1))
-- true

isError

Result.isError: (#some(a) | #error(e)) -> bool
Result.isError(#error("oops"))
-- true