为什么“案例类”不需要“新”来创建新对象

时间:2022-12-30 21:30:58

In Scala what is the reason that you don't need to use "new" to create a new "case class"? I tried searching for awhile now without answers.

在Scala中,您不需要使用“new”来创建新的“案例类”是什么原因?我试着寻找一段时间没有答案。

3 个解决方案

#1


37  

Do you want the how or the why? As the other answer notes, the how is just the apply method on the automatically generated companion object.

你想要的方式或原因?正如另一个答案所说,如何在自动生成的伴随对象上使用apply方法。

For the why: case classes are often used to implement algebraic data types in Scala, and the new-less constructor allows code that is more elegant (creating a value looks more like deconstructing it via pattern matching, for example) and that more closely resembles ADT syntax in other languages.

原因如下:案例类经常用于在Scala中实现代数数据类型,而无新构造函数允许更优雅的代码(例如,创建值看起来更像是通过模式匹配解构它)并且更接近类似其他语言的ADT语法。

#2


14  

Case class has prebuilt companion object with apply() implemented. Someone even complains about this: How to override apply in a case class companion :)

Case类具有预构建的伴随对象,并实现了apply()。有人甚至抱怨这个:如何覆盖案例类伴侣中的应用:)

#3


2  

Case classes provide you with an automatically generated apply function on their companion object that you can use like a constructor.

案例类为您的伴随对象提供了一个自动生成的应用函数,您可以像构造函数一样使用它。

In Scala decompiled byte code you will find apply function created as the following :

在Scala反编译的字节代码中,您将找到如下创建的应用函数:

object Person {
def apply(name: String, age: Integer): Person = new Person(name,age)
}

Example :

示例:

case class Person(name: String, age: Integer)

The following three all do the same thing.

以下三个都做同样的事情。

val p0 = new Person("Frank", 23) // normal constructor

val p1 = Person("Frank", 23) // this uses apply

val p2 = Person.apply("Frank", 23) // using apply manually

So if you use val p1 = Person("Frank", 23) it is not a constructor, this a method that call apply method.

因此,如果你使用val p1 = Person(“Frank”,23)它不是构造函数,这是一个调用apply方法的方法。

Please read scala-object-apply-functions for more info.

有关详细信息,请阅读scala-object-apply-functions。

#1


37  

Do you want the how or the why? As the other answer notes, the how is just the apply method on the automatically generated companion object.

你想要的方式或原因?正如另一个答案所说,如何在自动生成的伴随对象上使用apply方法。

For the why: case classes are often used to implement algebraic data types in Scala, and the new-less constructor allows code that is more elegant (creating a value looks more like deconstructing it via pattern matching, for example) and that more closely resembles ADT syntax in other languages.

原因如下:案例类经常用于在Scala中实现代数数据类型,而无新构造函数允许更优雅的代码(例如,创建值看起来更像是通过模式匹配解构它)并且更接近类似其他语言的ADT语法。

#2


14  

Case class has prebuilt companion object with apply() implemented. Someone even complains about this: How to override apply in a case class companion :)

Case类具有预构建的伴随对象,并实现了apply()。有人甚至抱怨这个:如何覆盖案例类伴侣中的应用:)

#3


2  

Case classes provide you with an automatically generated apply function on their companion object that you can use like a constructor.

案例类为您的伴随对象提供了一个自动生成的应用函数,您可以像构造函数一样使用它。

In Scala decompiled byte code you will find apply function created as the following :

在Scala反编译的字节代码中,您将找到如下创建的应用函数:

object Person {
def apply(name: String, age: Integer): Person = new Person(name,age)
}

Example :

示例:

case class Person(name: String, age: Integer)

The following three all do the same thing.

以下三个都做同样的事情。

val p0 = new Person("Frank", 23) // normal constructor

val p1 = Person("Frank", 23) // this uses apply

val p2 = Person.apply("Frank", 23) // using apply manually

So if you use val p1 = Person("Frank", 23) it is not a constructor, this a method that call apply method.

因此,如果你使用val p1 = Person(“Frank”,23)它不是构造函数,这是一个调用apply方法的方法。

Please read scala-object-apply-functions for more info.

有关详细信息,请阅读scala-object-apply-functions。