Can someone clarify why reified types behave this ...
# announcements
m
Can someone clarify why reified types behave this way?
s
What is printed when you call
println(ThingAndBoxedTypeRef.of<String>("a string").typeRef.type)
o
And what if you do
Copy code
inline fun <reified T> of(thing: T) =
    ThingAndBoxedTypeRef(thing, jacksonTypeRef<T>())
a
I get something like
Box<T>
output for that
it needs to be
jacksonTypeRef<Box<T>>()
to supply the explicit type parameter there
It seems like the way to solve this is to go to the TypeFactory (Jackson stuff) and construct the type manually:
Copy code
typeFactory.constructParametricType(Box::class.java, typeFactory.constructType(jacksonTypeRef<T>()))
Although this gives a
JavaType
rather than a
TypeReference
(which is just a type token way of making parameterised types easier to specify in source)
but that doesn’t actually answer the question of why it ended up this way
s
I wonder if making a named subclass of TypeReference and instantiating that would make a difference instead of the ad-hoc anonymous object.
Since these are literally inlined there’s no reason we couldn’t simplify this by making this a single top level function composition, that might provide more insight.
m
yep that pretty much summarizes my confusion: since it's all inlined it seems like it should all work out...
Avoiding jacksonTypeRef by creating the
object : TypeReference
didn't help