What is the difference between Kotlin data class and Scala case class?

5

In Scala we have case classes , for example:

case class Pessoa(nome: String, sobrenome: String)

and in Kotlin we have data classes :

data class Pessoa( val nome: String, val sobrenome: String )

What's the difference between the two?

    
asked by anonymous 10.07.2016 / 13:36

1 answer

5

Essentially they are used for the same purpose, that is, they define a record . By doing so they automatically gain the main required methods, including the "access" methods for the equation fields, hash code , textual representation ( toString ) and copy.

In Scala methods of apply() and unapply() are also provided for case class es.

In Kotlin, in data class , methods are provided that help to similar to unapply() , although less powerful, and language does not require the existence of apply() . It also has "accessorias" Java Beans in addition to the normal ones that are available for any class in Kotlin. You can not use inheritance in records. It requires the use of var or val for parameters to indicate that they are fields.

    
10.07.2016 / 16:12