Stewart Stewart
12/03/2021, 9:46 PMcatch
that only handles specific exceptions and throws the rest?thanh
12/05/2021, 10:58 AMinline fun <R> catch(f: () -> R, predicate: (Throwable) -> Boolean): Either<Throwable, R> =
try {
f().right()
} catch (t: Throwable) {
t.nonFatalOrThrow()
if (predicate(t.nonFatalOrThrow())) t.left()
else throw t
}
So we can use it like catch(f) {it as SpecificException}
Giulio Caccin
12/07/2021, 10:14 AMinline fun <A, B> Either<A, B>.filterLeft(predicate: (A) -> Boolean, default: () -> B): Either<A, B> =
swap().filterOrElse(predicate, default).swap()
to handle it in the flow, but i would like to improve this so i think also your catch
is greatStewart Stewart
12/19/2021, 6:42 PMas
result in a class cast exception?
I ended up playing with a version that used a type parameter, but it requires specifying R
explicitly as well.