How do I use `withError` to wrap a function with ...
# arrow
u
How do I use
withError
to wrap a function with
RaiseAccumulate<MyErrorA>
receiver, when the outer error context is
Raise<MyErrorB>
? When I try
Copy code
context(_: Raise<MyErrorB>)
fun validateOuter(x: X): X = withError({ errors -> MyErrorB(errors) } ) { validateInner(x) }
where
Copy code
context(_: RaiseAccumulate<MyErrorA>)
fun validateInner(x: X)
the type checker complains about no context argument found for
validateInner()
How can I tell
withError
to open a
RaiseAccumulate
context?
y
The easiest way would be:
Copy code
iorAccumulate(block).mapLeft(transform).bind()
u
Ok, thanks.
It doesn't look very elegant for what IMHO is a very common case when writing accumulating validation logic. Is there some glue machinery missing here?
y
It should definitely be added. Any name suggestions? I think just calling it
withError
might cause resolution issues (especially with context paramters)
u
withErrorNel
?
thank you color 1
Is there somewhere a clear explanation for the difference between
RaiseAccumulate<Error>
and
Raise<Nel<Error>>
? I find myself confused sometimes when trying to get the types line up
y
Previously,
RaiseAccumulate
was just a hidden
Raise<Nel<Error>>
, which can be accessed through
withNel
, and it exposes a
Raise<Error>
to make your life easier. With the new experimental DSL,
RaiseAccumulate
now has a whole
Accumulate
API, which allows you to accumulate errors as you go about your computation. That's what allows for binding
Ior
, since if you have a
Both
, the error should go somewhere. To go from
Raise<Nel<Error>>
to
RaiseAccumulate<Error>
, simply use the
accumulate { }
block.
👍 1