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
raiseto raise a typed error ofEmeaning that the computation has failed.ensureto raise a typed error ofEwhen predicate isfalse.ensureNotNullto raise a typed error ofEwhen value isnull.
Wrapper types
Either.Leftand.left()to wrap a value as error inEitherIor.Leftand.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.
zipOrAccumulateto operate over independent blocks, with potentially different types.mapOrAccumulateto operate over a collection of items.
Coroutines / suspended actions (guide)
Run several of them
Protect from potential problems
Schedule.retryto repeat an action until successful.Schedule.repeatto repeat an action, correctly handling problems.resourceScopefor 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
@opticsannotation and apply the KSP plug-in. - Collections: traversals that work on every element of a collections are available in the
Everyobject.
Obtain data
getobtains the single focused element. (Available forLens.)getOrNullobtains an optional focused element as nullable. (Available forOptional,Prism,Lens.)getAllobtains all focused elements asList. (Available for every optic.)foldMapcombines 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
modifyapplies an operation to every focused element.setchanges the value of every focused element.copyprovides a DSL to perform several modifications over the same value.
Generate a new value
reverseGetconstructs a value by means of aPrism.