Given the following code in Kotlin:
import com.fasterxml.jackson.module.kotlin.*
data class MyReply<R> (
val results : Array<R>? = null
)
class ErrorClient() {
val JSON = jacksonObjectMapper()
inline fun<reified R> request(): Array<R>? {
val json_in = """{"results": [2]}"""
val res: MyReply<R> = JSON.readValue(json_in)
return res.results
}
fun read(): Array<Int>? {
val res: Array<Int>? = request()
return res
}
}
and the following tests:
import org.junit.Test
class ErrorTest {
val client = ErrorClient()
@Test
fun `direct`() {
val res: Array<Int>? = client.request()
println(res)
}
@Test
fun `indirect`() {
val res : Array<Int>? = client.read()
println(res)
}
}
Short story: The first test passes and the second fails. Why?
Long story: I am experiencing a wrong type inference of the reified parameter R when calling the inline function via the read() class method, but the direct call to request() works. In the indirect case the type is erronously inferred to be java.lang.Object and thus the test fails with
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
at ErrorClient.read(Error.kt:17)
at ErrorTest.indirect(ErrorTest.kt:14)