bloder
06/06/2019, 12:19 PMraulraja
06/06/2019, 12:28 PM.get
is unsafe and there is history of folk complaining about methods like that being equivalent to null and NPEs. A safe version would be getOrElse
or simply a fold
. https://github.com/arrow-kt/arrow/blob/master/modules/core/arrow-core-data/src/main/kotlin/arrow/core/Either.kt#L212-L221bloder
06/06/2019, 12:31 PMalex.hart
06/06/2019, 12:44 PMget
sort of defeats the purpose of a Monad. Unwrapping data should be hard.bloder
06/06/2019, 12:51 PMcatch
would be considered the else
of getOrElse
then if you have a get()
inside of a try
and it didn't throwed any kind of exception, it would be considered a successful unwrapping of the Result
val value = try { valueResult().get() } catch(e: Exception) { Value() }
else
like:
val value = try { valueResult().composeError(::handleNetworkProblems).composeError(::handleSomeKindOfWrongValues).get() } catch(e: Exception) { Value() }
Robert Jaros
06/06/2019, 3:06 PMbloder
06/06/2019, 4:01 PMValidation
solution in this case where if I understood correctly we set some results on it and this Validation
struct is going to filter the errors, or we can call result.failure{}
that it's a simple callback to errors but in all these cases we need to do something like: when(it) {is NetworkException ->{//handle network case} is OtherException -> {// handle other case}}
imagine that with a lot of error cases and sub error checking, that's what I try to solve with this solutionfun result(n: Int): Result<Int> = if(n <= 3) {
Result.error(UnsupportedNumber())
.composeError(::handleWrongNumber)
} else {
Result.ok(n)
}
fun handleWrongNumber(e: UnsupportedNumber) = println("unsupported number")
result(2).get() // unsupported number
result(5).get() // 5