LeoColman
08/21/2019, 8:21 PMtry {
foo()
} catch(FooException) {
} catch(BarException) { }
streetsofboston
08/21/2019, 9:05 PMfun main() {
val tryValue: Try<Int> = Try { ... ... }
val t1: Try<Number> = tryValue
.onSpecificFailure(NumberFormatException::class) { -1 }
.onSpecificFailure(IOException::class) { 1.3 }
}
fun <T, T1 : T, T2 : T> Try<T1>.onSpecificFailure(tClass: KClass<out Throwable>, mapper: (Throwable) -> T2): Try<T> {
return when (this) {
is Try.Failure -> if (exception::class.isSubclassOf(tClass)) Try { mapper(exception) } else this
is Try.Success -> this
}
}
streetsofboston
08/21/2019, 9:08 PMBob Glamm
08/21/2019, 9:22 PMfun bar(): Int = TODO()
fun <F> MonadDefer<F>.foo(): Kind<F, Int> =
catch {
bar()
}.handleError { when(it) {
is IllegalArgumentException -> 4
is RuntimeException -> 5
else -> 6
}}