Chris Lee
11/30/2022, 3:26 PMprivate fun someFun() : Either<String,Int> {
return either.eager {
Either.catch({ it.message ?: "No message" }) {
42 // this would be a call hierarchy that may throw exceptions
}.bind() // Hmmm. need to unwrap either created from 'catch' to get a value for 'eager'
}
}
Sam
11/30/2022, 3:29 PMeither.eager { Either.catch(...).bind() }
the same as just Either.catch(...)
in this case?simon.vergauwen
11/30/2022, 3:29 PMEither.catch
+ mapLeft
(+ bind
) but in your case you you don't need either.eager
nor bind
.
private fun sampleFun(): Either<String, Int> =
Either.catch { 42 }
.mapLeft { it.message ?: "No message" }
Chris Lee
11/30/2022, 3:30 PMeither.eager
isn’t needed in this trivial example, but is required if you want to use ensure()
, ensureNotNull
, or are calling other methods that return eithers to be able to bind()
them.simon.vergauwen
11/30/2022, 3:32 PMeither {
val res = catch({ 42 }) {
raise(it.message ?: "No message")
}
}
simon.vergauwen
11/30/2022, 3:33 PMFlow#catch
where you can return a default value or raise another error.simon.vergauwen
11/30/2022, 3:34 PMChris Lee
11/30/2022, 3:34 PMsimon.vergauwen
11/30/2022, 3:34 PMChris Lee
11/30/2022, 3:35 PM