Add function to convert Deferred to suspending function
See original GitHub issueI have been debating about whether it’s better to inject Deferred<T>
or suspend () -> T
into my classes. The reason I’ve recently leaned to Deferred<T>
is I want to avoid allocating an extra anonymous inner class to convert Deferred<T>
to suspend () -> T
.
Now, I could create my own fun <T> Deferred<T>.asFunction(): suspend () -> T
to share, but I think this would be useful to add to the standard lib because I imagine many people may use it.
I think suspend () -> T
should be preferred over Deferred<T>
because the latter exposes the Job
API (which can be cancelled, and is therefore not read-only). Adding fun <T> Deferred<T>.asFunction(): suspend () -> T
to the standard lib may help encourage this behavior.
Example is below:
typealias GetIsUserAGoat = suspend () -> Boolean
@Suppress("FunctionName")
fun GetIsUserAGoat(scope: CoroutineScope): GetIsUserAGoat {
return scope.async {
true // pretend this is asynchronously fetched
}
.asFunction()
}
// add something like this to standard lib
fun <T> Deferred<T>.asFunction(): suspend () -> T = ::await
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Composing suspending functions - Kotlin
You can use .await() on a deferred value to get its eventual result, but Deferred is also a Job , so you can...
Read more >Convert Callback Into Kotlin Coroutines Suspend or Deferred
suspendCoroutine is a builder function that mainly used to convert callbacks into suspend functions easily.
Read more >What does the suspend function mean in a Kotlin Coroutine?
Suspending functions are at the center of everything coroutines. A suspending function is simply a function that can be paused and resumed at...
Read more >Kotlin Coroutines by Tutorials, Chapter 4: Suspending Functions
In this chapter, you'll learn more about how suspendable functions work from within. You will see how you can convert existing code, which...
Read more >Suspend what you're doing: Retrofit has now Coroutines ...
The adapter would convert a Retrofit Call in Deferred, on which you can invoke .await() in a CoroutineScope. Since this is now deprecated,...
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 Free
Top 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
It does create a separate class, yet I am not sure that the case for “reusing” this class with a dedicated one-line
asFunction
extension deserves it. We do not encourage the use ofDeferred
class in the first place.R8 should de-duplicate the classes that are created for that function reference. It’ll do it for lambdas, but I need to confirm it works with a bound function reference.