2011-07-11 226 views
2

爲什麼是它(在斯卡拉REPL)我可以寫,例如,斯卡拉地圖使用「_」

def double(d: Int) = 2*d 
(0 until 10).zipWithIndex.map(i => double(i._1)) 

或只是

(0 until 10).zipWithIndex.map(_._1) 

但我不能寫

(0 until 10).zipWithIndex.map(double(_._1)) 
error: missing parameter type for expanded function ((x$1) => x$1._1) (0 until 10).zipWithIndex.map(double(_._1)) 

+0

可能的重複:http://stackoverflow.com/questions/2173373/scala-foreach-strange-behaviour – sschaef

回答

11

斯卡拉試圖擴大_._1裏面double。因此,它認爲你想有

(0 until 10).zipWithIndex.map(double(i => i._1)) 

但是,它也看到了這i => i._1並沒有真正融入double的參數類型之一,因此它抱怨,並要求你給一個類型的提示,以幫助編譯器。但是,在這種情況下,不能有正確的類型定義,所以錯誤消息在那裏是錯誤的。