I’m trying to get up to speed with Kotlin. One of the things I keep hitting snags with is how functions and suspend functions feel like oil and water and don’t mix.
For example if I wanted to wrap some code like this:
time {
doSomething
}
I apparently can’t have it work regardless of whether doSomething is suspend or not:
package meh;
// I'm not really trying to time code; I'm actually trying to specialize assertFails
// This is just a minimum reproducible program that demonstrates where I'm getting stuck
fun time(fn: () -> Unit) = 0
fun time(fn: suspend () -> Unit) =
time { /* Overload resolution ambiguity between candidates
* fun time(fn: () -> Unit): Int
* fun time(fn: suspend () -> Unit): <ERROR TYPE REF: Ambiguity: time, [meh/time, meh/time]>
*/
runBlocking {
fn.invoke()
}
}
Is this type erasure and thus intractable, or am I just holding the language wrong? What’s the idiomatic way to work around this? do I call one of them something else, like timeBlocking or timeCoroutine or timeSuspend or blockAndTime or..? do I simply fail to factor out the runBlocking call from every applicable call site?
In case it matters, my team adopted Kotlin version 2.2.