https://kotlinlang.org logo
#announcements
Title
# announcements
d

Dico

09/26/2019, 6:13 PM
Let's say I have these 2 Java methods:
public String get(Integer key) {...}
public String get(int key) {...}
How can I call one of these without running into overload resolution ambiguity?
d

Dominaezzz

09/26/2019, 6:15 PM
I think, with a nullablilty hack you can call the first one. Not sure how to call the second one.
d

Dico

09/26/2019, 6:16 PM
ah that does work, I want to call the second one though 😅
k

karelpeeters

09/26/2019, 6:16 PM
So
get(5)
picks the first one as well?
d

Dico

09/26/2019, 6:17 PM
that's illegal
k

karelpeeters

09/26/2019, 6:17 PM
Right 🤦‍♂️. What does
val f: (Int) -> String = ::get
do? Also illegal?
d

Dico

09/26/2019, 6:17 PM
The concrete use case is that I want to call
remove(key, value)
on a
Int2ObjectOpenHashMap
from fastutil
d

Dominaezzz

09/26/2019, 6:17 PM
Ha! Beat me to it.
d

Dico

09/26/2019, 6:18 PM
Also illegal
I can do
get(5 ?: null)
or
get(5 as Int?)
but that doesn't call the one I want to call
k

karelpeeters

09/26/2019, 6:19 PM
There's reflection and creating a Java wrapper but you know about those simple smile
d

Dico

09/26/2019, 6:19 PM
I can create a Java utility function I guess..
k

karelpeeters

09/26/2019, 6:24 PM
Exact same problem here (it's even fastutil): https://stackoverflow.com/q/37625599/5517612 It looks like there was a solution back then, maybe it's a regression?
d

Dico

09/26/2019, 6:28 PM
Hmm, something weird seems to be happening here
```
Copy code
public static <V> boolean remove(Int2ObjectMap<V> map, int k, V v) {
        return map.remove(k, v);
    }

    public static <V> boolean remove(Int2ObjectMap<V> map, Integer k, V v) {
        return map.remove(k, v);
    }
I made these and I can call the primitive one from Kotlin
but not the member method
d

Dominaezzz

09/26/2019, 6:30 PM
Same project maybe. 🤷🏼
d

Dico

09/26/2019, 6:31 PM
I think it may be because of inheritance
the problem doesn't exist with
put
, which has the same overload ambiguity
making very little sense
thanks for the help to you both
k

karelpeeters

09/26/2019, 6:35 PM
I think there's a document somewhere specifying the exact resolution order/steps, but I can't seem to find it. I'll search when I'm on a pc.
Do let us know if you find a solution, now I'm interested!
d

Dico

09/26/2019, 6:36 PM
I have no answer to why there's an error calling
remove
but not
put
I am adding a wrapper function though to make sure it calls the right one
Today I found out that comparing equality of inline classes causes both to be boxed when looking at the bytecode
😮 1
k

karelpeeters

09/26/2019, 6:37 PM
Haha why would that happen!?
d

Dico

09/26/2019, 6:37 PM
so I lost some trust in the Kotlin compiler's decision making
They fixed this for next version though I think
k

karelpeeters

09/26/2019, 6:38 PM
I feel like inline classes are still very brittle, especially around delegates I faced some issues too.
d

Dico

09/26/2019, 6:38 PM
Yeah they are an experimental feature of course
I think you know the project I'm using them on
👍 1
k

karelpeeters

09/26/2019, 6:39 PM
Right, I can confirm they deserve that status.
d

Dico

09/26/2019, 6:40 PM
I wonder what it does when comparing unsigned ints now 🤔
k

karelpeeters

09/26/2019, 6:42 PM
What use case do you have for them? I have trouble coming up with anything other than unsigned ints and packed coordinates.
d

Dico

09/26/2019, 6:42 PM
References to memory from another process
k

karelpeeters

09/26/2019, 6:43 PM
That's neat.
d

Dico

09/26/2019, 6:44 PM
I've been working on replacing the inline class system over the last few days 😕
It was definitely great to start with but hasn't scaled too well
it's great though for defining remote memory structures like this
k

karelpeeters

09/26/2019, 6:45 PM
The memory reference system? Or do you have some general inline class stuff?
d

Dico

09/26/2019, 6:45 PM
the prior
can confirm it doesn't box UInt when comparing two
👍 1
k

karelpeeters

09/26/2019, 6:52 PM
Then maybe
a >= b && a <= b
is faster.
The JVM might even realize what's going on an optimize that, but I'm not so sure.
d

Dico

09/26/2019, 6:52 PM
p >= nullptr && nullptr <= p
😂
k

karelpeeters

09/26/2019, 6:53 PM
That's even easier to optimize!
d

Dico

09/26/2019, 6:54 PM
There were millions and millions of boxed inline class instances in memory snapshots because of that comparison
k

karelpeeters

09/26/2019, 6:55 PM
Really? I've been led to believe the JVM was good at optimizing short lived instances too, idls there an actual performance impact?
d

Dico

09/26/2019, 6:57 PM
if you go higher than 15 million you will definitely feel it
I can't give you numbers but I can say that the application lagged
I think I read somewhere that the GC will freeze the application if things get out of hand or something
k

karelpeeters

09/29/2019, 1:23 PM
This was the document I was talking about earlier, unfortunately it doesn't really say anything about the primitive mapping: https://github.com/JetBrains/kotlin/blob/master/spec-docs/NameResolution.adoc
d

Dico

09/30/2019, 4:18 AM
Interesting read!