I have been using Kotlin for 2 years and never had this problem, but now I have met TensorFlow and a new problem has appeared.
The problem resides in a class of TensorFlow called org.tensorflow.DataType. This class converts Java’s raw type wrappers (such as Integer or Float) into TensorFlow types INT32, FLOAT.
It works by storing Wrapper classes in a map of <Class<?>, DataType>.
So whenever some code call this from Kotlin:
or this from java:
An IlegalArgumentException is thrown saying: “int objects cannot be used as elements in a TensorFlow Tensor”, so the only way to do it work from is with a wrapper class like:
This is not a problem in Java, due to the auto-casting, but in Kotlin, it forces you to migrate from Kotlin basic types to Java types, this is horrible, because kotinc sees all Java compiled sources primitive types as Kotlin basic types, so your project becomes a complete mess, with tons of code for changing from kotlin basic types to Java primitive types and vice-versa.
In this case, the problem is easily solved with reflection, but the problem would be bigger if the values in the HashMap were hard-coded with a switch.
So what should we do for other cases if the problem can not be solved just with reflection?