bbaldino
07/09/2020, 11:25 PMKClass<T>
and returns a function which takes a String
and returns an instance `T`:
fun <T : Any> getterFor(valueType: KClass<T>): (String) -> T
One issue with this is that, due to type erasure, it doesn't work for things like List<Int>
. I know `typeOf`/`KType` can help with this, but is there a typesafe way I can refer to a KType
argument's type in the return type position? I.e. before I have T
which matches between the argument (KClass<T>
) and the return type T...is there a way to do that with KType?
fun getterFor(type: KType): (String) -> ???
bbaldino
07/09/2020, 11:27 PMfun <T : Any> getterFor(type: KType): (String) -> T
and then cast to T
, but wondering if there's a way to 'enforce' a relationship between the KType and the return value's typeZach Klippenstein (he/him) [MOD]
07/09/2020, 11:42 PMinline fun <reified T : Any> getterFor(): (String) -> T {
@Suppress("UNCHECKED_CAST")
return getterFor(typeOf<T>()) as (String) -> T
}
@PublishedApi internal fun getterFor(type: KType): (String) -> Any {
…
}
bbaldino
07/09/2020, 11:43 PMbbaldino
07/09/2020, 11:44 PM