【发布时间】:2014-06-26 18:42:54
【问题描述】:
考虑以下几点:
class A(foo: Int)(bar: Int)(baz: Int)
object A{
def apply(foo: Int)(bar: Int)(baz: Int) = new A(foo)(bar)(baz)
}
使用 apply 方法,我可以执行以下操作:
scala> A(1)(2)(3)
res12: Script.A = Script$A@7a6229e9
scala> A(1)_
res13: Int => (Int => Script.A) = <function1>
为什么我不能执行以下操作:
scala> new A(1)_
<console>:21: error: missing arguments for constructor A in class A
new A(1)_
^
我是否遗漏了一些语法方面的东西?我认为构造函数只是类上的方法,因此在需要时应该将它们提升为函数(很像上面的 apply 方法)
【问题讨论】:
-
根据你使用它的方式,定义你的构造函数 uncurried 可能更容易,然后使用
.curried。见this answer -
希望 Scala 的未来版本能够消除这些警告并变得更加一致......