I've tried many approaches, nothing seems to work.
# codereview
b
I've tried many approaches, nothing seems to work.
t
I don't think there is a solution. The root of your problem is the <*> in the Map, which means all typing information is already lost. Using something like Map<KClass<T>, T> would be a solution, but that is not possible in Kotlin (see this question on SO, which also discusses alternatives). In the code you provided I don't see how the data is populated. If you control that part and can guarantee the type safety yourself just slap a
Copy code
@Suppress("UNCHECKED_CAST")
on there. Otherwise either use as? with nullable return type or be fine with throwing exceptions.
b
Thanks. That makes sense. I even tried reified generics with an inline function to no avail. I wonder if I just switch to some kind of wholly functional approach, maybe I can magic up some above-the-JVM generics system.
m
type-safety in this situation is achieved through a THC, o better, SymbolMap: https://stevewedig.com/2014/08/28/type-safe-heterogenous-containers-in-java/
👍 1
b
Interesting! Thanks Matteo, I’m checking it out
👌 1