https://kotlinlang.org logo
#codereview
Title
# codereview
b

Byron Katz

02/28/2021, 3:09 AM
I've tried many approaches, nothing seems to work.
t

Timmy

02/28/2021, 2:17 PM
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

Byron Katz

02/28/2021, 2:23 PM
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

Matteo Mirk

03/01/2021, 4:00 PM
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

Byron Katz

03/01/2021, 8:37 PM
Interesting! Thanks Matteo, I’m checking it out
👌 1
2 Views