Skip to content

Either introduce getOrElseThrow method #3598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mipo256 opened this issue Mar 14, 2025 · 7 comments
Open

Either introduce getOrElseThrow method #3598

mipo256 opened this issue Mar 14, 2025 · 7 comments

Comments

@mipo256
Copy link

mipo256 commented Mar 14, 2025

I have not found any appropriate issue, so I'm leaving it here.

So, assume we have the following function that returns the karrow Either:

fun <T> send(body: T) : Either<ExternalFailure, ExternalSuccess> {
    // implementation
}

What it does is it sends over a specific protocol some information, it does not matter for this issue. What I need is to call the function send(body: t), and I need to throw a RuntimeException to the callee, in case we hit an error. This is the contract that I have to adhere to.

The way I should recognize that the error had happened is by checking if the Either is Left. So I need something like this:

return send(myBody).throwOnLeft({ left ->
    ProtocolIOException("Something bad had happened :${left}")
})

I do not need to mapLeft, or getOrElse or anything - I just need to return the ExternalSuccess (the Right in my case), or I need to throw an exception.

Is there any built-in way to do so?

@mipo256
Copy link
Author

mipo256 commented Mar 14, 2025

I can provide a PR if needed. We have implemented this function as an extension to karrow Either

@kyay10
Copy link
Collaborator

kyay10 commented Mar 14, 2025

That's just:

return send(myBody).getOrElse { left ->
    throw ProtocolIOException("Something bad had happened :${left}")
}

@mipo256
Copy link
Author

mipo256 commented Mar 14, 2025

Yes, that can be done this way, I know, but this function is in general meant to compute the default value in case we have Left. The lambda that is passed is of type (A) -> B, where the return type is B.

You are correct @kyay10, but my point is that it may be a good idea to have a dedicated method for it? I'm not insisting, just sharing my thoughts that calling getOrElse was not obvious from the first glance to solve the problem

@kyay10
Copy link
Collaborator

kyay10 commented Mar 14, 2025

Part of the intention of having such a type is that you can pass in an (A) -> Nothing function, e.g. one that throws an exception. I think this is a wider Kotlin trend that we shouldn't deviate too hard from (for instance, nullableFoo ?: error("foo is null") is idiomatic Kotlin, and similarly send(myBody).getOrElse { error("response was $it") })

@mipo256
Copy link
Author

mipo256 commented Mar 14, 2025

@kyay10 So, should I try to submit the PR and we'll see how it goes?

@mipo256
Copy link
Author

mipo256 commented Mar 31, 2025

ping here @kyay10 :)

@serras
Copy link
Member

serras commented Apr 13, 2025

I agree here with @kyay10, the idiomatic way to handle this in Kotlin is writing getOrElse { throw ... }, and we would like to keep the API as close as possible to these idioms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants