在构建器模式中使用 apply /(数组下标)方法

Using the apply / (array subscripting) method in a builder pattern

给定一个返回数组的简单函数:

scala> def methodReturnsArray() = { Array(1.0, 2.0) }
methodReturnsArray: ()Array[Double]

我们可以继续调用函数:

scala> val myarr = methodReturnsArray
myarr: Array[Double] = Array(1.0, 2.0)

scala> myarr(0)
res21: Double = 1.0

但是不能直接使用apply(/数组索引语义):

scala> methodReturnsArray(0)
<console>:53: error: too many arguments for method methodReturnsArray: ()Array[Double]
              methodReturnsArray(0)
                                ^

请求是解释为什么 不可行。其次:是否有某种方法可以获得 "inline" 调用。 IE。不需要分开步骤:s

在单独的声明中。

啊哈!发布后几秒钟,我得出了答案:需要在函数调用中包含括号:即 methodReturnsArray () (0) :

scala> methodReturnsArray()(0)
res22: Double = 1.0