Typed errors (guide)
Run a computation with potential errors
For any given value of val program: Raise<E>.() -> A
, the following functions are available:
Handle any potential outcomes
recover(program, error)
to recover from any error.fold(program, error, success)
to handle error and success case, and re-throw any exceptions.fold(program, exception, error, success)
to handle exception, error and success case.
Obtain the result as a wrapper type
These functions create a Raise
scope where all the operators in this section become available.
either(program)
to obtainEither<E, A>
.result(program)
to obtainResult<A>
, the error type is fixed toThrowable
.nullable(program)
to obtainA?
, the error type is fixed toNull
.option(program)
to obtainOption<A>
, the error type is fixed toNone
.
Embed any potential errors in the block
- If the call has
Raise<E>
as receiver, nothing is needed. - If the call returns a wrapper type (
Result
,Either
, ...), call.bind()
.
Indicate that an error occurred
DSL syntax
raise
to raise a typed error ofE
meaning that the computation has failed.ensure
to raise a typed error ofE
when predicate isfalse
.ensureNotNull
to raise a typed error ofE
when value isnull
.
Wrapper types
Either.Left
and.left()
to wrap a value as error inEither
Ior.Left
and.leftIor()
to wrap a value as error inIor
Handle potential errors (guide)
These functions allow raising errors of the same type that the surrounding block.
recover(program, error)
to recover from any error.catch(program, exception)
to perform some action when the block throws an exception.
Accumulate errors (guide)
These functions use NonEmptyList<E>
as the surrounding error type, or take (E, E) -> E
as the error accumulator.
zipOrAccumulate
to operate over independent blocks, with potentially different types.mapOrAccumulate
to operate over a collection of items.
Coroutines / suspend
ed actions (guide)
Run several of them
Protect from potential problems
Schedule.retry
to repeat an action until successful.Schedule.repeat
to repeat an action, correctly handling problems.resourceScope
for correct acquisition and release of resources.
For more resilience options check the corresponding section in the docs.
Immutable data (guide)
Generate optics
- User-defined data: mark the class with the
@optics
annotation and apply the KSP plug-in. - Collections: traversals that work on every element of a collections are available in the
Every
object.
Obtain data
get
obtains the single focused element. (Available forLens
.)getOrNull
obtains an optional focused element as nullable. (Available forOptional
,Prism
,Lens
.)getAll
obtains all focused elements asList
. (Available for every optic.)foldMap
combines all the focused elements in a single element. (Available for every optic.)
The Traversal
type offers an API closely matching that of Iterable
.
Copy value with modified data
modify
applies an operation to every focused element.set
changes the value of every focused element.copy
provides a DSL to perform several modifications over the same value.
Generate a new value
reverseGet
constructs a value by means of aPrism
.