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 function to convert Deferred to suspending function

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
elizarovcommented, Sep 7, 2019

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 of Deferred class in the first place.

1reaction
JakeWhartoncommented, Sep 7, 2019

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.

Read more comments on GitHub >

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

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