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 <em>not</em> 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.


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 <em>by default</em>? 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:


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

</li> <li>

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.

</li> <li>

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

</li> <li>

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.

</li> </ol>

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.



  • Why do data classes not implement Serializable?
  • Test CoroutineScope infrastructure in Kotlin
  • Why addDefaultArguments() removed from Navigation Component version 1.0.0-alpha09? What is WorkAroun
  • Using mkdir -m -p and chown together correctly
  • FizzBuzz Ruby one-liner
  • Issue a POST request with url_for in Flask
  • What does “2
  • Neo4j Cypher WITH is required between CREATE and MATCH
  • Javascript calculate quantity * price
  • How to generate automatic properties (get, set) for Visual Studio 2008 C++
  • How to access native iOS file system using Sencha touch 2 alone
  • web scraping in R
  • Navbar active tab change color when scrolled
  • How can I configure security per node in Corda using Gradle?
  • Can I store name, address, and LAT/LONG from Google Places API in my own database
  • Code Map Missing in Visual Studio 15 - Preview 4
  • Spotfire Custom Expression : Calculate (Num/Den) Percentages
  • openOptionsMenu() across android versions
  • Creating Neural Network for un-encountered inputs
  • R: Hide dummies output
  • Footer appears next to section instead of below
  • How do I use RestSharp to POST a login and password to an API?
  • Firebase Cloud Messaging: how to send data message to all users? [duplicate]
  • Open default mail app from within Qt with some html
  • Using a custom URL rewriter, IIS6, and urls with .htm, .html, etc
  • -(void)viewWillAppear:(BOOL)animated doesn't called
  • IE doesn't display png images
  • Complex multiple if statements
  • Unable to connect to Azure MySQL Database through Azure Function - C#
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Regex not working in java 1.5
  • PHPMailer return to AJAX
  • Conflicting declaration using constexpr and auto in C++11
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Call Microservice from another Microservice within Docker