简体   繁体   中英

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 ?

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 object s exist.

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

Scala case class creates a class which:

  1. Defines accessor functions (getters and setters basically)
  2. Overrides naturally hashcode , toString and equals functions
  3. Provides a copy function in order to create in an easy way shallow copies.

Kotlin data class does pretty much the same thing as Scala case class :

  1. Defines accessor functions (getters and setters basically)

  2. Overrides naturally hashcode , toString and equals functions

  3. Provides a copy function in order to create in an easy way shallow copies.

The main differences between the two is the fact that Scala provides a more powerful pattern matching feature compared to Kotlin (in fact Kotlin doesn't have real pattern matching).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM