Hi ! Is there any way to write a function which ta...
# serialization
m
Hi ! Is there any way to write a function which takes any Serializable class like :
fun doSomething(input: Serializable) { Json.encodeToString(input) }
? My current solution is to use a serializable
sealed interface
which force me to write all my serializable data in the same package.
e
you could do something like:
Copy code
inline fun <reified T> doSomething(input: T): String {
  serializerOrNull(typeOf<T>())?.let { serializer ->
    Json.encodeToString(serializer, input)
  } ?: error("No serializer for ${T::class.simpleName}")
}
m
Thanks ! it works but I'm looking for a solution which throw an error at the compile, not at the runtime.