I try to call a custom Java collection from Kotlin, which implements an add(long value)
method in this way:
public class SomeSet<T> {
public void add(int value) { }
public void add(long value) { }
public void add(T value) { }
}
In Java we can call the overloaded method without any problems:
SomeSet<Long> set = new SomeSet<Long>();
set.add(1);
set.add(1L);
But in Kotlin it seems to be impossible:
val set = SomeSet<Long>()
set.add(1 as Int)
set.add(1L) // ⚡ Overload resolution ambiguity
set.add(1L as Long) // ⚡ Overload resolution ambiguity
Complete compile error:
Overload resolution ambiguity. All these functions match.
public open fun add(value: Long): Unit defined in de.itscope.platform.producttracking.test.SomeSet
public open fun add(value: Long!): Unit defined in de.itscope.platform.producttracking.test.SomeSe
I already checked out suggests, for a very likely problem:
Kotlin: What can I do when a Java library has an overload of both primitive and boxed type?
But this doesn't helps here, whit this generic overloading.
ℹ This is not academic example. It's the way the
com.google.zetasketch.HyperLogLogPlusPlus
class is implemented. A HyperLogLog++ implementation from google out of the ZetaSketch library.