dave08
03/13/2023, 4:27 PMfun foo() = effect {
raise(DomainError)
}
fun bar() = effect {
val context = ....
val result = foo().bind()
}
data class WrappedError(val context: Context, val error: DomainError)
// elsewhere
bar().fold({ e -> // I need e to be WrappedError but I can only get the context in bar() ... and foo() raises DomainErrors w/o the context. }) { ... }
simon.vergauwen
03/13/2023, 4:31 PMbar
should return Effect<WrappedError, ?>
? You can use recover
inside of effect { }
(or as top-level anywhere else).
fun bar() = effect {
val context = ...
val result = recover({
foo().bind()
}) { e: DomainError -> raise(WrappedError(context, e)) }
}
dave08
03/13/2023, 4:34 PMsimon.vergauwen
03/13/2023, 4:35 PMcontext
outside of effect { }
you can also do foo().recover { e: DomainError -> raise(WrappedError(context, e)) }
dave08
03/13/2023, 4:35 PMsimon.vergauwen
03/13/2023, 4:42 PMdave08
03/13/2023, 4:46 PMsimon.vergauwen
03/13/2023, 4:49 PMwith
, apply
, ?.let
, withContext
, withTimeout
, etc. Unrelated to FP, or OOP.dave08
03/13/2023, 4:55 PMwith
or apply
, it won't necessarily be reused, the function just speaks the domain language better (and at the same time avoids some of the nesting...). I just didn't bother separating it until I got to this nesting.