I'm excited for the objc interop but I'm a bit puz...
# kotlin-native
p
I'm excited for the objc interop but I'm a bit puzzled about how it's actually supposed to work. How are two objc selectors
popcorn:withSalt:
and
popcorn:withSugar:
distinguished on the kotlin side?
🤔 1
It seems they would be callable as
popcorn(foo, withSalt=1)
and
popcorn(foo, withSugar=2)
on the kotlin side. But what does
popcorn(foo, 1)
mean then?
This does not compile (conflicting overloads, overload resolution ambiguity) but works on the kotlinc REPL:
Copy code
fun foo(a: Int, b:Int) : Int {
   return a + b
}

fun foo(a: Int, c:Int) :Int {
   return a - c
}

fun main(args : Array<String>) { 
    println(foo(1,2))
    println(foo(a=1, b=2))
    println(foo(a=1, c=2))
}
So it seems kotlin can distinguish methods (functions) based on the parameter names (the REPL can). But the compiler does not like it, because it's dangerous and ambigous. (kotlin-native is still building on my machine so I have not tried this yet with kotlin-native. I just looked at the interop code and the samples, and was curious how this is supposed to work)
s
It seems they would be callable as
popcorn(foo, withSalt=1)
and
popcorn(foo, withSugar=2)
on the kotlin side.
Yes, exactly.
But what does
popcorn(foo, 1)
mean then?
popcorn(foo, 1)
means nothing, because it is ambiguous. The compiler doesn’t accept this.
kotlin-native is still building on my machine
Please note that we have’ve just released Kotlin/Native 0.4, mostly corresponding to current state of
master
. So you probably can just download the built release.
p
Thank you for your answer. That’s quite interesting. Does
popcorn(foo, 1)
work, if there is only one matching objc selector (just
popcorn:withSalt:
not
...withSugar:
existing)? (I’ve seen lot’s of objc interop approaches to different languages and I’m always curious how the fundamental question of selector->function_call is answered 😉 And thank you for mentioning the binaries. I was not aware of them.
o
But the compiler does not like it, because it’s dangerous and ambigous.
it’s not the Kotlin compiler, it’s JVM that doesn’t allow this.
o
yes, we rely upon Kotlin's ability to use overload by parameter name (usually disabled, but we enable it by @file:Suppress("CONFLICTING_OVERLOADS"). And surely, if no selector conflict exists, parameter name can be omitted. Feel free to experiment with smth like
Copy code
@file:Suppress("CONFLICTING_OVERLOADS")
fun foo(a: Int, b:Int) : Int {
   return a + b
}

fun foo(a: Int, c:Int) :Int {
   return a - c
}

fun main(args: Array<String>) {
  println(foo(a=2, c=1))
  println(foo(a=2, b=1))
  //  Doesn't compile.
  // println(foo(a=2, 1))
}
p
Thank you for the additional information.