https://kotlinlang.org logo
Title
b

Bernhard

11/21/2018, 10:16 AM
I would have expected the inliner to just copy the code as is and file in all the type parameters with the real type variable
d

diesieben07

11/21/2018, 10:18 AM
Can you clarify what you mean? Type parameters are still erased, there is no
Class<T>
or
Class<String>
after compilation, only
Class
. But if you do
T::class
with a
reified T
, then you will get the proper
Class
instance.
You can't do that with a normal type parameter
b

Bernhard

11/21/2018, 10:25 AM
exactly, you can do T::class
for instance in Gson you can parse a list of things by using:
mapper.fromJson<ArrayList<AdditionalFunction>>(it, object : TypeToken<ArrayList<AdditionalFunction>>() {}.type)
what I tried to do was to generalize it like this:
mapper.fromJson<ArrayList<T>>(it, object : TypeToken<ArrayList<T>>() {}.type)
where T is reified
d

diesieben07

11/21/2018, 10:28 AM
That should work™ 😄
b

Bernhard

11/21/2018, 10:28 AM
and call it like this:
func<AdditionalFunction>()
turn out it deserializes to a linked hashmap instead of a List<AdditionalFunction>
when you println what TypeToken<ArrayList<T>>() {}.type evaluates to, you get “ArrayList<T>>”
instead of “ArrayList<AdditionalFunction>”
so it feels like T is only reified if you access it via ::class
and inlining happens after type checking and is type erased
d

diesieben07

11/21/2018, 10:33 AM
It seems like the compositing of types causes the issue here. The following prints `ArrayList<String>`:
private inline fun <reified R> a() {
    println(object : TypeToken<R>() {})
}

fun main(args: Array<String>) {
    a<ArrayList<String>>()
}
b

Bernhard

11/21/2018, 10:34 AM
compiler bug?
or works as expected?
d

diesieben07

11/21/2018, 10:37 AM
Not sure. Sounds like a bug to me, but I am not sure if it's possible to do properly.