下划线_在Scala中广泛应用,_的基本含义是作为占位符使用。_在使用时是出问题非常多的地方,本文将不断完善_的使用场景以及所表达的含义
1. 在高阶函数中使用
scala> val list = List(-3,8,7,9)
list: List[Int] = List(-3, 8, 7, 9)
scala> list.filter(_ > 7)
res8: List[Int] = List(8, 9)
_ > 7是一个函数字面量,_表示调用这个函数时提供的参数值,按理说,应该指定类型,但是这里没有指定,原因是可以通过filter操作推导出来
如下写法不对:
scala> list.filter(_:Int=>_>7)
<console>:1: error: identifier expected but integer literal found.
list.filter(_:Int=>_>7)
^
2. 省略函数列表
scala> val f = (_:Int) + (_:Int)
f: (Int, Int) => Int = <function2>
scala> f(2,3)
res11: Int = 5
scala> val f=(x:Int,y:Int)=>x+y
f: (Int, Int) => Int = <function2>
scala> f(2,3)
res12: Int = 5
通过val f = (_:Int) + (_:Int),我们只定义了函数f的方法体,并没有指定函数的参数列表,_和_分表表示第一个和第二个参数,
_ + _ expands into a literal for a function that takes two parameters.
限制: You can use this short form only if each parameter appears in the function literal at most once. (我认为是exactly once)Multiple underscores mean multiple parameters,not reuse of a single parameter repeatedly. The first underscore represents the first parameter, the second underscore the second parameter, the third underscore the third parameter, and so on.
如下定义错误:
scala> val f = _ + _
<console>:7: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
val f = _ + _
^
<console>:7: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
val f = _ + _
^
但是如下操作正确
scala> val list = List(-3,8,7,9)
list: List[Int] = List(-3, 8, 7, 9)
scala> list.reduce(_ + _)
res13: Int = 21
3. 部分应用函数
scala> val f = (a:Int,b:Int,c:Int)=>a+b+c
f: (Int, Int, Int) => Int = <function3>
scala> val sum = f _
sum: () => (Int, Int, Int) => Int = <function0>
如下写法错误:
scala> val sum = f(1) _
<console>:9: error: not enough arguments for method apply: (v1: Int, v2: Int, v3: Int)Int in trait Function3.
Unspecified value parameters v2, v3.
val sum = f(1) _
^
如下写法正确
scala> val sum = f(1,_:Int,_:Int)
sum: (Int, Int) => Int = <function2>
如下写法错误:
scala> val sum = f(1,_,_)
<console>:9: error: missing parameter type for expanded function ((x$1, x$2) => f(1, x$1, x$2))
val sum = f(1,_,_)
^
<console>:9: error: missing parameter type for expanded function ((x$1: <error>, x$2) => f(1, x$1, x$2))
val sum = f(1,_,_)
^
原因是,f函数可能会有重载,假如f有两个具有三个参数的重载函数,那么调用f(1,_,_),编译器将不知道调用哪个函数将第一个参数(这里是1)代入,而f _则没有参数代入的步骤,这个在后期进行参数应用时,才会找具体的f
关于这个问题,http://stackoverflow.com/questions/8549393/redundant-parameter-type-info-in-partially-applied-function-definition有讨论