Yannick Lazzari
04/27/2022, 4:13 PMfun <A,B> Either<A, B>.throwException(f: (A) -> Unit): Unit = when (this) {
is Either.Left -> f(this.value)
else -> Unit
}
which I then use like this:
val someResult: Either<SomeErrorADT, String> = ...
someResult.throwException {
when (it) {
SomeErrorADT.Error1 -> throw SomeException1("...")
SomeErrorADT.Error2 -> throw SomeOtherException("...")
}
}
Is there an existing function or extension function that exists in Arrow that kind of does the same thing that I’m not seeing?phldavies
04/27/2022, 4:22 PMtapLeft
(or even handleError
) should do the job
someResult.tapLeft {
when(it) {
SomeErrorADT.Error1 -> error("💥")
SomeErrorADT.Error2 -> error("🐍")
}
}
Yannick Lazzari
04/27/2022, 4:24 PMEither
when in this case it’s clear we’ll never use the return type. But thanks for the info.simon.vergauwen
04/28/2022, 6:39 AMimperative parts that still rely on Exceptions being thrown to manage control flow. I find myself leaning on an extension function that looks like this:That is sometimes indeed a use-case, but it's especially the case when introducing Arrow in existing codebases but it's too niche to promote it in Arrow itself. It's also against the motivation behind Arrow, so we only promote safe patterns such as Phil Davies suggested.
simon.vergauwen
04/28/2022, 6:40 AM