Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala case class vs Kotlin data class

Scala has a feature called case class, while Kotlin has another feature called data class. Which are the main differences between Scala case class and Kotlin data class?

like image 881
Dina Bogdan Avatar asked Dec 18 '22 13:12

Dina Bogdan


1 Answers

Overall they are very similar, but there are some differences I'd mention:

  1. Scala case class can have multiple parameter lists (including implicit parameters), and only parameters from the first list are used for toString/equals/hashCode.

  2. Scala allows a case class to have no parameters, Kotlin doesn't. Of course, usually such a case class should be an object instead.

  3. On that note, case objects exist.

  4. The companion object of a case class extends the corresponding function type by default.

like image 68
Alexey Romanov Avatar answered Jan 06 '23 04:01

Alexey Romanov