question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add default value for the failure param in the result.fold

See original GitHub issue

Description

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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
SleeplessBytecommented, Nov 8, 2018

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… SogetOrElse did the trick for non-nullable types.

override suspend fun getAlbumList(): List<AlbumDomainModel> { val (_, _, result) = Fuel.get(“/albums”).awaitObjectResponse<List<AlbumNetworkModel>>(

gsonDeserializerOf())

    var albums = result.getOrElse(listOf())
    return albums.map { it.toDomainModel() }
}

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 getOrNull method? 🤔

override suspend fun getAlbum(id: Int): AlbumDomainModel? {
    val (_, _, result) = Fuel.get("/albums/$id").awaitObjectResponse<AlbumNetworkModel>(gsonDeserializerOf())

    var album: AlbumNetworkModel? = null
    result.fold(success = { album = it }, failure = {})

    return album?.toDomainModel()
}

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/kittinunf/Fuel/issues/494#issuecomment-436831554, or mute the thread https://github.com/notifications/unsubscribe-auth/AB35WEnNdJwC6Ne15Xf9WbebJR-B_F3oks5us35WgaJpZM4YKWWw .

0reactions
igorwojdacommented, Nov 8, 2018

I had some time off… SogetOrElse did the trick for non-nullable types.

override suspend fun getAlbumList(): List<AlbumDomainModel> {
        val (_, _, result) = Fuel.get("/albums").awaitObjectResponse<List<AlbumNetworkModel>>(gsonDeserializerOf())

        var albums = result.getOrElse(listOf())
        return albums.map { it.toDomainModel() }
    }

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 new retult.getOrNull method ? 🤔

    override suspend fun getAlbum(id: Int): AlbumDomainModel? {
        val (_, _, result) = Fuel.get("/albums/$id").awaitObjectResponse<AlbumNetworkModel>(gsonDeserializerOf())

        var album: AlbumNetworkModel? = null
        result.fold(success = { album = it }, failure = {})

        return album?.toDomainModel()
    }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found