Is there a way to overload a function's suspension? ```fun foo(() -> Unit): Unit = ... suspend f...
r
Is there a way to overload a function's suspension?
Copy code
fun foo(() -> Unit): Unit = ...
suspend foo(suspend () -> Unit): Unit = ...
For example, to make
foo(..)
suspend only if its argument is suspend also. The above code does compile, but
foo { }
is ambiguous.
e
no 😞 if you don't want to write the implementation twice, you could
Copy code
private inline fun fooImpl(() -> Unit): Unit = ...
fn foo(() -> Unit): Unit = fooImpl(...)
suspend fun foo(suspend () -> Unit): Unit = fooImpl(...)
👍 1
l
If the function is inline, that's already the default behavior. That's how you can suspend within blocks passed to apply, also, with, let, and run.