Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do data classes not implement Serializable?

I am new to kotlin but a veteran of scala. The former has impressed me so far. But at this moment I am working through a head scratcher - that the data class does not implement Serializable. How a class with that name could expected to not be routinely expected to be used in that manner eludes me.

What are the technical challenge(s) that precluded that support? I'm interested because i'd like to create a wrapper. It seems that the expectation is to always provide a serializer() ? That is cumbersome and actually kotlinx.serialization is not working for me yet - even after some effort.

like image 927
WestCoastProjects Avatar asked Apr 08 '20 06:04

WestCoastProjects


People also ask

Why is Serializable not implemented?

2. The reason the java. lang. Object didnt implement Serializable is because, what if you do NOT want to make certain fields as Serializable and you my mistake missed to add transient to that field, then will be a havoc.

What happens if a class implements Serializable?

If a super class implements Serializable, then its sub classes do automatically. When an instance of a serializable class is deserialized, the constructor doesn't run. If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run.

Why do classes implement Serializable?

Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class. Serializable classes are useful when you want to persist instances of them or send them over a wire.

What happens if we do not implement Serializable?

If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.


1 Answers

It is a bit unclear from the question if you mean java.io.Serializable (based on the analogy with Scala and "implement") or kotlinx.serialization.Serializable (based on the second paragraph and discussion in the comments).

First, for the Java one:

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.

Then you can note this isn't the only place where Kotlin requires you to be more explicit than Scala. For another data class example, you need to mark properties by val or var where Scala assumes val by default. 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 Serializable in particular:

  1. It would privilege Java serialization which has bad reputation (as mentioned in the comments already).

  2. Serializable isn't usable in cross-platform (or just Kotlin/JS or Kotlin/Native) projects. Maybe data classes could only be serializable on JVM, but it would be an unnecessary mismatch between platforms.

  3. case classes implement Serializable even if they have non-Serializable properties and will throw if you actually try to serialize them.

  4. 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.

For 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. I wouldn't be surprised if data classes do become @Serializable by default when it "graduates" like coroutines did in 1.3.

like image 133
Alexey Romanov Avatar answered Oct 03 '22 17:10

Alexey Romanov