Add default value for the failure param in the result.fold
See original GitHub issueDescription
result.fold
should have a default value for failure
parameter. Since return
statement is not allowed in success
and failure
blocks now we need to define a nullable variable albums
to store it.
private suspend fun httpGetCoroutine(): List<AlbumNetworkModel> {
var albums: List<AlbumNetworkModel>? = null
Fuel.get("/albums").responseObject<List<AlbumNetworkModel>> { _, _, result ->
result.fold(
success = {
albums = it
},
failure = {
albums = listOf()
})
}
return albums ?: listOf()
}
Proposed Solution
However, with the default value for failure
parameter, we could use the library in a more expressive way by assigning a default value to our albums
variable
private suspend fun httpGetCoroutine(): List<AlbumNetworkModel> {
var albums: List<AlbumNetworkModel> = listOf()
Fuel.get("/albums").responseObject<List<AlbumNetworkModel>> { _, _, result ->
result.fold(
success = {
albums = it
})
}
return albums
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Result - Kotlin Programming Language
fold. Returns the result of onSuccess for the encapsulated value if this instance represents success or the result of onFailure function for the...
Read more >Either - Arrow-KT
Left value to a Either.Right using the value of Left. This can be useful when mapping to a single result type is required...
Read more >Result Folding - Coveo Documentation
Result folding allows you to group items that have a certain field value in common, and identify parent-child relationships between those items in...
Read more >rust - Returning default value from function when result is error
I tried using unwrap_or_else() , but that just returns the closure not the outer function. i.e. try_decrypt_data(params.get("pass").
Read more >Arrow Try is dead, long live Kotlin Result | by Juan Rada
The fold function allows mapping Result value or exception. It's really useful when, for example, a default value needs to be used in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Responses should always have data or expect explicitly to not have data. “null” is a really bad indicator of no data and an empty list is much more expressive, but if you MUST you can use List<AlbumDomainModel>?. If you get an object with an non existing idea, you really should get back either an empty list or a 404 (which is an error).
I doubt
getOrNull
will be supported, because the whole point of Result is to NOT have to deal with null values.On Thu, 8 Nov 2018 at 01:44 Igor Wojda notifications@github.com wrote:
I had some time off… So
getOrElse
did the trick for non-nullable types.However, I am still not sure how to simplify response that may not contain data (try to get object with non-existing id).
getOrElse
method does not allow to return null values, so either I am missing something or we need newretult.getOrNull
method ? 🤔