https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Paul Woitaschek

10/09/2019, 7:26 AM
Why donesn't this compile?
Copy code
fun functionImpl(a: Int, b: String) {}

fun nonSuspending(function: (Int, String) -> Unit) {}
fun suspending(function: suspend (Int, String) -> Unit) {}

fun test() {
  nonSuspending(::functionImpl)
  suspending(::functionImpl)
}
m

Marc Dietrichstein

10/09/2019, 7:35 AM
fun suspending
requires a
suspend
function but you pass
functionImpl
which is not a
suspend
function The following code works for example:
Copy code
fun functionImpl(a: Int, b: String) {}
suspend fun functionImplSuspending(a: Int, b: String) {}

fun nonSuspending(function: (Int, String) -> Unit) {}
fun suspending(function: suspend (Int, String) -> Unit) {}

fun test() {
    nonSuspending(::functionImpl)
    suspending(::functionImplSuspending)
}
p

Paul Woitaschek

10/09/2019, 7:36 AM
I know; but why?
The design resolution is that conversion from regular functions to suspending function will be supported.
Looks like it will be implemented. But it's from '17. I just refactored several classes from rxjava to flow and this made the migration a pain @elizarov
e

elizarov

10/09/2019, 11:01 AM
Tentatively in 1.4.0. It requires a new type inference engine.
5 Views