Is there any way to be able to overload a function...
# language-proposals
p
Is there any way to be able to overload a function with another one that receives a suspended function? Like:
Copy code
fun doSomething(() -> Unit)
and
Copy code
fun doSomething(suspend () -> Unit)
I understand that, at least on JVM implementations, this is not possible since both
() -> Unit
and
suspend () -> Unit
are the “same” because of type erasure. Both are instances of
Function0
However, it could be useful when, as an API provider, we want to provide the same api for both without having to use namespacing (like adding a prefix or a postfix) 🙂 Same goes with non coroutine examples like if we want to have overload of operations with generics. Like
doSomething(intList: List<Int>)
and
doSomething(stringList: List<String>)
If not currently, is there a plans to add this as a language feature? Maybe with namespacing them at compile time.
c
You could try to use the
@JvmName
(or so) annotation to change the name of one of the functions — maybe will work?
p
Even with different Jvm names it doesn’t seem to work 😭
I thought about that one, as that works for the example I gave for the Lists 🙃 There are other cases where these don’t work, like having nullable receivers and not nullable receivers, but I guess that’s a different problem
e
These are actually already different types on JVM. The problem is that overload resolution is not taking
suspend
modifier into account and this is currently done on purpose, but might get relaxed in a future. Please, follow this issue: https://youtrack.jetbrains.com/issue/KT-23610
p
True, they are Function0 and Function1. 🙃 Continuation args 😅 Thanks for the issue, definitely will give it a follow...
As a note aside, calling a HOF with a suspend argument with a function that is not suspend generates all the boiler plate for such suspension m maybe this ticket could solve that an avoid unnecessary boiler plate generation. Or should that be a separate ticket?