Given a reified type parameter T, if that type is a List, how can I get the list's item type?
I tried overloading the reified method with T : Any and T : List and also TItem : Any, TList: List<TItem> but that doesn't compile due to conflicting overloads.
Fails to compile, conflicting overloads
inline fun <reified T> getRequiredVariable(variableName: String) =
// ... implementation
inline fun <reified TItem, reified TList : List> getRequiredVariable(variableName: String) =
// ... implementation
Failed workaround attempt
inline fun <reified T : Any> getRequiredVariable(variableName: String) =
if (T::class.isSubclassOf(List::class)) {
val tKClass = T::class
val itemType = tKClass.typeParameters[0] // Won't work, even though T is reified, the type parameter apparently is not
val itemClassifier = itemType.createType().classifier
val itemKClass = itemClassifier as KClass<*> // Fails, KTypeParamaterImpl is not KClass<*>
getRequiredListVariable(variableName, itemKClass) as? T
} else {
getRequiredVariable(variableName, T::class)
}