https://kotlinlang.org logo
#getting-started
Title
# getting-started
p

phldavies

07/28/2022, 10:59 PM
Can anyone shed some light as to why the compiler is raising an “Overload resolution ambiguity” in the following?
Copy code
fun CtxA.action() = CtxA
fun CtxC.action() = Unit

context(CtxA, CtxB)
fun bothContexts() {
    // Overload resolution ambiguity. All these functions match.
    // public fun CtxA.action(): CtxA defined in root package in file CtxRcv.kt
    // public fun CtxC.action(): Unit defined in root package in file CtxRcv.kt
    action()
}

context(CtxA)
fun onlyCtxA() = action() // works fine
As far as I can tell, it shouldn’t even be considering
CtxC.action
as a candidate as it’s not in scope. I can qualify as
this@CtxA.action()
but obviously not ideal.
y

Youssef Shoaib [MOD]

07/29/2022, 12:35 AM
It's even worse:
Copy code
context(Unit, Boolean)
fun bothContexts() {
  // Refers to String.toInt() for some reason
  toInt()
}
when you run it, it fails with:
Copy code
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.VerifyError: Bad local variable type
Exception Details:
  Location:
    ContextExtensionsKt.bothContexts(Lkotlin/Unit;Z)V @6: aload_2
  Reason:
    Type top (current frame, locals[2]) is not assignable to reference type
  Current Frame:
    bci: @6
    flags: { }
    locals: { 'kotlin/Unit', integer }
    stack: { }
  Bytecode:
    0x0000000: 2a12 feb8 0011 2cb8 0104 57b1
Definitely report this on Youtrack because it is a major issue with the resolution algorithm (it seems that whenever there's a context receivers group (instead of a singular context receiver) the compiler allows any method to be called inside)
😯 5
p

phldavies

07/29/2022, 7:51 AM
I'll raise this once I'm at my desk, if someone hasn't beaten me to it!