does `@serializable` make the class implement an i...
# serialization
c
does
@serializable
make the class implement an interface? i would like to have a generic method accept only serializable classes, something like
Copy code
<T : Serializable>
m
No, but you could provide the serializer as an argument:
Copy code
fun <T> serializeSomething(serializer: KSerializer<T>) {
  // ...
}
Then call it like this:
serializeSomething(Person.serializer())
c
yes thats what I’m doing now, was thinking if theres a way to avoid it.
e
You can also make an inline reified version of it and use
serializer()
to lookup the serializer at compile-time based on the compile-time type, thus removing the need to pass serializer explicitly in your API
c
do you mean
Copy code
T::class.serializer()
?
e
No. I mean, top-level
serializer<T>()
where
T
is reified.
(it works with lists all the other stuff that cannot be recovered from a class)
m
Oh, nice.
Copy code
inline fun <reified T> serializeSomething(t: T) {
    val serializer = serializer<T>()
    // ...
}
e
yes
c
the javadocs say
* This is a computation-heavy call, so it is recommended to cache its result.
e
It only matters if you do it a lot. (like sending millions messages per second). Also, we are working on intrinsifying it (to move it fully to compile-time)
👍 8
c
ok, great thanks!