scala - Scala中什么时候需要新的

标签 scala new-operator

Possible Duplicate:
“new” keyword in Scala

Ive noticed that creating certain instances in Scala you can leave off using new . When is it required that a developer use new ? And why do some objects allow you to miss off using it?

Thanks

List("this","is","a","list") creates a List of those four strings; no new required

Map("foo" -> 45, "bar" ->76) creates a Map of String to Int, no new required and no clumsy helper class.

Taken from here ..

最佳答案

通常,scala集合类使用apply方法在其伴随对象中定义工厂方法。 List("this","is","a","list")Map("foo" -> 45, "bar" ->76)是调用那些apply方法的语法糖。使用此约定是相当惯用的scala。

另外,如果定义了case class C(i: Int),它还将定义一种工厂C.apply(i: Int)方法,可以将其称为C(i)。因此,不需要新的东西。

除此之外,new是创建对象所必需的。

关于scala - Scala中什么时候需要新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9763908/

相关文章:

html - 视频网站的 PlayFramework 导航栏

javascript - 使用 jQuery 制作新函数 javascript(不起作用)

c++ - 分配两次 - c++

scala - 扩展泛型类型 - PriorityQueue

javascript - Playframework:通过Javascript通过location.href调用 Controller 的action

c# - 以最简单的方式实例化一个对象数组?

c++ - C++ 中的 operator new、new_handler 函数

reactjs - 无法读取未定义 react 17 的属性 'map'

mysql - 使用 Scala 解析 AWS Glue 中的 MySQL 0000-00-00 日期格式

scala - 如何使用 Google Guice 的 @Inject 和 Scala/(Play 2.4.x)