2011-07-11 228 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的参数类型之一,因此它抱怨,并要求你给一个类型的提示,以帮助编译器。但是,在这种情况下,不能有正确的类型定义,所以错误消息在那里是错误的。