kotlin-result: Binding doesn't support coroutines

I was excited to try out the binding block in our codebase but it doesn’t support suspending functions. We currently use Result heavily with coroutines so this was a bit of a bummer. Is there any likelihood of this being added in the future? I’m not sure if it’s out of scope for this project. I’m happy to raise a PR if you think it would be a worthwhile addition.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 21 (12 by maintainers)

Most upvoted comments

@badvok666 @ditn regarding

IIRC you’re having issues because you’re using a method reference. We use andThen with suspending functions all the time.

It looks like when we bump this lib to 1.4 that will no longer be an issue, see section “Suspend conversion on callable references”:

https://kotlinlang.org/docs/reference/whatsnew14.html#callable-reference-improvements

Released in 1.1.8

@Munzey

The convention of making a function an extension on CoroutineScope is that it launches a new coroutine in some form (e.g. it calls launch). The binding function should be a suspend fun but shouldn’t actually launch a coroutine itself, therefore making it an extension function on CoroutineScope would be breaking the convention that Roman tells us to follow in this presentation.

Okay, sounds like an acceptable approach to implement it with a different name. I think putting it in a different package with the same name is more likely to cause confusion. The name suspendBinding will do for now I think. If somebody is willing to make a PR with this and the appropriate tests (as mentioned, we are going to need a test-only depenedency on kotlinx-coroutines as a result), then we can go ahead and progress this. Bonus points if we can get some benchmarking results in the PR as @Munzey eluded to, as it would give us some useful insight.

one thing that’s bugging me - its a pain that the kotlin compiler can’t disambiguate between the names if the suspend version of the function is named the same. it compiles the functions fine, youll just not be able to use them 😓 the “clean” solution to me is to write an extension

suspend inline fun <V, E> CoroutineScope.binding(...) : Result<V, E>

but that would require pulling the kotlin coroutine common core into the main dependencies. @michaelbull would that be a deal breaker to you? otherwise i will continue ahead with naming it suspendBinding (or i thought to keep the binding word front of mind, bindingWithSuspend or bindingSuspend as its already quite a long keyword)

Ah thank you. I was planning on taking a look this weekend or next week, but I suspect it’ll be quicker if you do it.

just an fyi, if someone does want to go ahead and open a pr for this, because its an mpp project, the coroutine dep you will need for testing is kotlinx-coroutines-core-common