![]() I wouldn't be surprised if data classes do become by default when it "graduates" like coroutines did in 1.3. In the common case of multiple case classes extending a trait, if you forget to make the trait extends Product with Serializable type inference often gives ugly types.įor Kotlin Serialization, the answer is even simpler: you don't want a basic language feature like data classes to depend on an experimental and immature library. Maybe data classes could only be serializable on JVM, but it would be an unnecessary mismatch between platforms.Ĭase classes implement Serializable even if they have non- Serializable properties and will throw if you actually try to serialize them. The body and headers can contain arbitrary children in a different namespace. Serializable isn't usable in cross-platform (or just Kotlin/JS or Kotlin/Native) projects. Basically an example use case (my real one is even more complex) is the use of JAXB (or alternative approach) to deserialize SOAP envelopes. It would privilege Java serialization which has bad reputation (as mentioned in the comments already). ![]() ![]() And for non-data classes, Scala allows you to use non- val constructor parameters inside the class effectively promoting them to private val where Kotlin doesn't. For another data class example, you need to mark properties by val or var where Scala assumes val by default. Then you can note this isn't the only place where Kotlin requires you to be more explicit than Scala. I think the way you should phrase it is: why do data classes not implement Serializable by default? You can always add : Serializable if you want. It is a bit unclear from the question if you mean java.io.Serializable (based on the analogy with Scala and "implement") or (based on the second paragraph and discussion in the comments).
0 Comments
Leave a Reply. |