Higher order function can take function as parameter and/or return them as result.
Given the higher order function:
fun highOrder(func:()->Unit ){
println("this is a higher order function")
func()
}
There are two ways to use it:
fun main() {
highOrder( ::doSomething )
highOrder { doSomething() }
}
Given:
suspend fun getFromServer(): String {
println("Working Hard!")
delay(4_000)
return "Server Response"
}
why am i unable to execute coroutine in same manner?
this is possible:
CoroutineScope(Dispatchers.IO).launch (block = { getFromServer() })
this is NOT:
CoroutineScope(Dispatchers.IO).launch (block = ::getFromServer )
While I understand this is not the recommended approach for launching coroutines, I’m curious why the latter implementation isn’t working. How can I make it work?
the block
parameter is looking for
suspend CoroutineScope.() -> Unit
is it possible to define a function that obeys that?