Reputation: 624
How to return suspend function from regular function? How to create suspend function instance?
For example, I can return async result from function:
fun <T> f(g: () -> T): Deferred<T> = GlobalScope.async { g() }
But I notices Kotlin allow the following signature of function:
fun <T> f(g: () -> T): suspend () -> T {
TODO()
}
How can I implement it?
Upvotes: 1
Views: 1854
Reputation: 3745
@Rene's answer but shorter:
fun <T> f(g: () -> T) = suspend { g() }
Upvotes: 3
Reputation: 6258
One way to do it:
fun <T> f(g: () -> T): suspend () -> T {
suspend fun intern() = g()
return ::intern
}
Upvotes: 3