christophsturm
07/09/2020, 9:25 AM@serializable
make the class implement an interface? i would like to have a generic method accept only serializable classes, something like
<T : Serializable>
marstran
07/09/2020, 9:52 AMfun <T> serializeSomething(serializer: KSerializer<T>) {
// ...
}
Then call it like this: serializeSomething(Person.serializer())
christophsturm
07/09/2020, 9:53 AMelizarov
07/09/2020, 9:54 AMserializer()
to lookup the serializer at compile-time based on the compile-time type, thus removing the need to pass serializer explicitly in your APIchristophsturm
07/09/2020, 9:57 AMT::class.serializer()
?elizarov
07/09/2020, 9:59 AMserializer<T>()
where T
is reified.elizarov
07/09/2020, 9:59 AMmarstran
07/09/2020, 9:59 AMinline fun <reified T> serializeSomething(t: T) {
val serializer = serializer<T>()
// ...
}
elizarov
07/09/2020, 9:59 AMchristophsturm
07/09/2020, 10:01 AM* This is a computation-heavy call, so it is recommended to cache its result.
elizarov
07/09/2020, 10:02 AMchristophsturm
07/09/2020, 10:03 AM