2010-10-27 83 views
1

注意:對於更一般問題的詳細答案在Stack Overflow問題What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?具有咖喱功能的Scala無點呼叫語法

以下工作:

scala> List(1,2,3) filter (_ > 1) reduceLeft(_ + _) 
res65: Int = 5 

而且還有如下:

scala> List(1,2,3).filter(_ > 1).foldLeft(0)(_ + _) 
res67: Int = 5 

而不是現在這樣的語法時才:

scala> List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _) 
<console>:10: error: 0 of type Int(0) does not take parameters 
     List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _) 
             ^

什麼是修復建議?

回答

7

本主題中的堆棧溢出問題What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?有很好的描述。

Curried功能似乎比具有一個參數的方法稍難一點。爲了省略點,curried函數需要在中綴調用之外使用括號。

由於Marimuthu Madasamy mentioned,這個工作(對象(表),方法(foldLeft)和它的第一個參數(0)是在括號中):

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _) 
+1

Marimuthu,你應該加入你的答案。 – mkneissl 2010-10-27 12:50:44

4

這工作:

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)