I would have expected the inliner to just copy the...
# announcements
b
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
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
exactly, you can do T::class
for instance in Gson you can parse a list of things by using:
Copy code
mapper.fromJson<ArrayList<AdditionalFunction>>(it, object : TypeToken<ArrayList<AdditionalFunction>>() {}.type)
what I tried to do was to generalize it like this:
Copy code
mapper.fromJson<ArrayList<T>>(it, object : TypeToken<ArrayList<T>>() {}.type)
where T is reified
and call it like this:
Copy code
func<AdditionalFunction>()
d
That should work™ 😄
b
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
It seems like the compositing of types causes the issue here. The following prints `ArrayList<String>`:
Copy code
private inline fun <reified R> a() {
    println(object : TypeToken<R>() {})
}

fun main(args: Array<String>) {
    a<ArrayList<String>>()
}
b
compiler bug?
or works as expected?
d
Not sure. Sounds like a bug to me, but I am not sure if it's possible to do properly.