可以说我们有一个函数def fun(x: X): X => Y
,我们通过fun _
而不是fun
将这个函数作为参数传递给另一个函数。我明白fun _
实际上是一个函数值,而fun
是指一个函数定义。在Scala中,乐趣_和乐趣之间的区别是什么
例如让:
val a = List(1,2,3,4)
def fun(x: Int) = {println(x); x + 1}
然后运行:
//This line works
a.map(fun _)
//This one also works, even though "fun" is not a function value
a.map(fun)
它们具有相同的输出:
1
2
3
4
resX: List[Int] = List(2, 3, 4, 5)
在大多数情况下,他们似乎工作一样,是有没有任何函数值不等于函数定义的例子?
'fun'是一种方法,而不是一种功能。区别很重要。 – rightfold
的可能的复制[什么是Scala的ETA膨胀?](http://stackoverflow.com/questions/39445018/what-is-the-eta-expansion-in-scala) – Haspemulator
发布一个实际的例子示出了“我们通过这个函数作为另一个函数的参数,使用'fun _'而不是'fun'“和”大部分它们看起来都是一样的“。这对我们来说会更容易。 http://stackoverflow.com/help/mcve – YoungSpice