9

For example, FastUtil's IntArrayList has a push method that accepts both int (primitive) and Integer (boxed), but Kotlin sees these both as the same function push(Int), therefore I cannot use the function at all as the function is ambiguous.

What should I do when a Java library has overloads for both the primitive and boxed type?

(p.s. I am aware that I can use the add(int) method. I am in search of what to do if I come across such a problem in the future.)

Jire
  • 9,680
  • 14
  • 52
  • 87

2 Answers2

10

Consider these methods in Java:

void f(int x) { }
void f(Integer y) { }

In Kotlin, they are seen as

f(x: Int)
f(x: Int!)

The second method has a parameter of platform type, meaning it might be nullable, corresponding to Integer.

First one can be simply called with Kotlin Int passed:

f(5) // calls f(int x)

To call the second one, you can cast the argument to nullable Int?, thus selecting the overload:

f(5 as Int?) // calls f(Integer y)
hotkey
  • 140,743
  • 39
  • 371
  • 326
  • 1
    This does not seem to be working with Kotlin 1.0.2 stable (`x` is an `Int`): http://i.imgur.com/KLVy5Vn.png – Jire Jun 04 '16 at 02:54
  • 1
    @Jire, ah, indeed. I had tested it only with Java sources within the the same project, and it had worked for that case. The problem is that for `push(Integer)` the Kotlin signature is `push(Int)` for some reason and not `push(Int!)`. – hotkey Jun 04 '16 at 03:01
  • 1
    @Jire, this turned out to be a bug in the compiler. – hotkey Jun 04 '16 at 11:10
  • 2
    This solution doesn't work as of `v1.0.3` but apparently does work on `v1.1-M01` – Enno Shioji Jul 25 '16 at 12:50
  • @hotkey Even in v1.1-M01 you can't call the primitive version if you want to use an argument – Jire Sep 10 '16 at 00:28
  • 1
    @Jire, agreed, but [the related issue](https://youtrack.jetbrains.com/issueMobile/KT-11440) has been marked as fixed and has target version 1.0.4, so the fix will hopefully be released soon. – hotkey Sep 10 '16 at 07:26
  • This problem is referenced in [Effective Java Item #41](https://github.com/mgp/book-notes/blob/master/effective-java-2nd-edition.markdown#item-41-use-overloading-judiciously). The author recommends just this: either cast your argument to the boxed type (e.g. `5 as Int?`) or use some other means to force your argument into the boxed type (e.g. invoke `Integer.valueOf(5)`). – B W Oct 09 '17 at 14:28
1

Have you tried writing a Java class to serve as an intermediary? That is, write your own java class with the method you want Kotlin to see, then call that method from your Kotlin code.

user2297560
  • 2,953
  • 1
  • 14
  • 11