Nathan Bedell
11/06/2021, 2:26 PM@Serializable(with=...)
), serialization of my data class will fail at run-time, instead of warning me at compile-time that my data class is not serializable.
Coming from experience with type-class/trait based approaches to serialization, this is really jarring to me in practice. One of the nice things to me about automatically deriving serializer/deserializers pairs is that it eliminates a whole class of unit/property-based tests I would otherwise have to write to ensure correctness of my program. If there's a possibility my program could crash at run-time because I tried to serialize something that isn't serializable, I still need to write those tests, which can be very tedious/time consuming.
However, when I asked a question and suggested at least allowing for users to "opt-out" of the "interfaces are automatically serializable" approach", I got the response:
"Open polymorphism is a powerful mechanism that is tied to the runtime by its nature, so giving up on compile-time errors is a tradeoff for convenience."
So, I have a couple of questions for the community here:
1. Does anyone understand this design decision, and exactly why this decision from the kotlinx.serialization team is convenient?
2. Is anyone else equally bothered as me from the lack of compile-time type safety in the current implementation of kotlinx.serialization?
For 2, perhaps if there is enough support we could open a new feature request specifically asking to be able to opt out of automatic interface serialization at run-time.
If the Kotlin team holds to their grounds, there's always the possibility that someone could make a fork, or a more properly type-class based approach, but I'd like to avoid that if possible. Less fragmentation is good!simon.vergauwen
11/06/2021, 8:29 PMJson
object with the correct factories since in the actual runtime it will still use reflection rather than statically injected instances.