Look at this example:
fun f(block: suspend () -> Unit) {
println("with suspend")
}
fun f(block: () -> Unit) {
println("without suspend")
}
fun main() {
f(suspend {
})
// This call cause compilation error:
// Error:(16, 5) Kotlin: Overload resolution ambiguity:
// public fun f(block: () -> Unit): Unit defined in root package in file Main.kt
// public fun f(block: suspend () -> Unit): Unit defined in root package in file Main.kt
//
// f({
// })
// This call cause compilation error:
//
// Error:(25, 5) Kotlin: Overload resolution ambiguity:
// public fun f(block: () -> Unit): Unit defined in root package in file Main.kt
// public fun f(block: suspend () -> Unit): Unit defined in root package in file Main.kt
//
// f {
// }
}
Here is declared two functions (one with suspend
keyword in lambda and one without).
Questions:
1) How to call first or second function? As you can see I can call function with suspend
but can't call function without suspend
keyword.
2) It is possible to rewrite f(suspend {})
with trailing lambda (i.e. use something like f suspend {}
)?