Restriction for type suspend () -> Unit


() -> Unit
is not
suspend () -> Unit

Are there any reason and are any support for conversion comming?


Please read the documentation on suspended functions first. These two functions are different and and have different platform signature since suspended function has coroutine context as an implicit parameter. In most cases you can use normal functions instead of suspended ones, but not vice versa.


Why I can’t use normal functions instead of suspended ones always?

For example:

fun work(func: suspend ()->Unit ){…}
fun func1(){}
suspend fun func2(){}

work(::func1) // error
work(::func2) //ok


I think this is the limitation of method references. As I already said, signature is different. Try to use direct lambda, it will work.


yes, it works

work{ func1() }

thanks :slight_smile:


but not very convenient, need to repeat params, twice

fun work(func: suspend (p1:P1,p2:P2,p3:P3)->Unit ) {}

fun func1(p1:P1,p2:P2,p3:P3){}
suspend fun func2(p1:P1,p2:P2,p3:P3){}

work(::func1)  // error
work(::func2)  // ok

work{p1,p2,p3-> func1(p1, p2, p3)}  //ok, but ugly