[英]Scala case class vs Kotlin data class
Scala has a feature called case class , while Kotlin has another feature called data class . Scala 有一个叫做case class的特性,而 Kotlin 有另一个叫做data class 的特性。 Which are the main differences between Scala case class and Kotlin data class ? Scala案例类和 Kotlin数据类之间的主要区别是什么?
Overall they are very similar, but there are some differences I'd mention:总的来说,它们非常相似,但我要提到一些不同之处:
Scala case class can have multiple parameter lists (including implicit parameters), and only parameters from the first list are used for toString
/ equals
/ hashCode
. Scala case 类可以有多个参数列表(包括隐式参数),并且只有第一个列表中的参数用于toString
/ equals
/ hashCode
。
Scala allows a case class to have no parameters, Kotlin doesn't. Scala 允许 case 类没有参数,而 Kotlin 不允许。 Of course, usually such a case class should be an object
instead.当然,通常这样的案例类应该是一个object
。
On that note, case object
s exist.就此而言, case object
s存在。
The companion object of a case class extends the corresponding function type by default. case 类的伴生对象默认扩展相应的函数类型。
Scala case class creates a class which: Scala case 类创建了一个类,它:
Kotlin data class does pretty much the same thing as Scala case class : Kotlin数据类与 Scala案例类几乎相同:
Defines accessor functions (getters and setters basically)定义访问器函数(基本上是 getter 和 setter)
Overrides naturally hashcode , toString and equals functions自然地覆盖hashcode 、 toString和equals函数
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).两者的主要区别在于,Scala 提供了比 Kotlin 更强大的模式匹配功能(实际上 Kotlin 没有真正的模式匹配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.