So I'm trying to create a generic SQLDelight ColumnAdapter class with a subtype of
SettingsCollection
as a parameter, but from what I understand, KotlinX.Serialization requires a reified type parameter, which isn't available at the class level. I don't really understand generics and reification if I'm honest. Does anyone have any pointers? Is this even possible?
Copy code
class SettingsColumnAdapter<T: SettingsCollection>(val collectionType: T) : ColumnAdapter<T, String> {
override fun decode(databaseValue: String): T {
return Json.decodeFromString(databaseValue)
}
override fun encode(value: T): String {
return Json.encodeToString(value)
}
}
d
DonizeteVida
03/21/2023, 9:17 PM
Are you trying to use reflection or some compile time generated implementation?
DonizeteVida
03/21/2023, 9:17 PM
Reflection isn't available on T type, even in pure Java.
DonizeteVida
03/21/2023, 9:19 PM
reified parameter is a T generic parameter which will be translated as a KClass of T type.
It isn't available in classes, but only in inlined functions. As these will be copied into source code.
DonizeteVida
03/21/2023, 9:21 PM
you should do something like:
class IWantSomeReflectionOfT<T : Any>(clazz: KClass<T>) {}
inline fun <retified T> buildAboveClass() = IWantSomeReflectionOfT(T::class)