Is it possible to create a function overload with ...
# announcements
l
Is it possible to create a function overload with the suspend modifier without changing packages? I would like to create something like
Copy code
fun foo(fn: () -> Unit) {}
suspend fun foo(fn: suspend () -> Unit) {}
but in the same package
s
No, because as far as the JVM is concerned, those two functions have the same signature. It’s like return types, the callsite can’t easily distinguish between the two
l
And if I change the JVM name it still doesn't work
Oh, I see... The caller can't distinguish
Dang.
And I suppose there isn't a way to say "This function may be suspend if called from a suspend context, and not suspend if not"
s
Not that I’m aware of. You could call one
foo
and the other
fooBlocking
l
I see. Thanks for the insight
c
The JVM actually sees two different signatures. The
suspend
function actually has an implicit
Continuation
parameter. But the caller can't distinguish, exactly.
l
What I tried to do was inline the function, so that it would use the suspension context it is in
But I use reflect inside
foo
with
fn
, so that doesn't work.
Can anything be done about it?