Why doesn't suspend function support overloading?

The following code is

A compilation error occurs with the message

Conflicting overloads: fun func(): Unit

suspend fun func() {

}

fun func() {

}

But when you decompile, the two functions are clearly different.

@Nullable
   public static final Object func(@NotNull Continuation $completion) {
      return Unit.INSTANCE;
   }

public static final void func() {
   }

The suspend method has a Continuation object declared as a parameter.
If so, isn’t there a distinction between non-suspend methods and method signatures?

It’s because the two declarations are conflicting when called. If you call foo() inside a suspend function, which should that call resolve to? The question is ambiguous enough that the compiler throws its hands up and just tells you to deal with it

1 Like

Thank you for your reply.
If you think about it from the Kotlin compiler’s perspective, it was a problem that could be easily solved :slight_smile:

Why would you even need this? What’s a legitimate use-case?

Of course it can be easily solved, and in fact Kotlin has made some ambiguity-resolving choices in other areas, but this one was deemed too semantically ambiguous for the person reading and writing the code

1 Like

I don’t think it’s easily solved. If I call func() from within a coroutine, which version should be called? Both are perfectly valid function calls in that context, so the compiler has no idea which one I want.

3 Likes