下划线符号“”在Scala语言中经常会用到,并且出现的场景千变万化。本文就列举出在一些主流情境下,“”这个魔法符号分别是什么用法。
当匿名函数传递给方法或其他函数时,如果该匿名函数的参数在=>的右侧只出现一次,那么就可以省略=>,并将参数用下划线代替。这对一元函数和二元函数都适用。例:
scala> val list = List(5,3,7,9,1)
list: List[Int] = List(5, 3, 7, 9, 1)
// list.map(x => x * 10)
scala> list.map(_ * 10)
res0: List[Int] = List(50, 30, 70, 90, 10)
// list.sortWith((x, y) => x < y)
scala> list.sortWith(_ < _)
res1: List[Int] = List(1, 3, 5, 7, 9)
// list.reduceLeft((x, y) => x + y)
scala> list.reduceLeft(_ + _)
res2: Int = 25
当匿名函数的参数未被实际使用到时,可以不给它一个命名,而直接用下划线代替。例:
scala> list.foreach(_ => println("Hello Scala"))
Hello Scala
Hello Scala
Hello Scala
Hello Scala
Hello Scala
泛型定义中的通配符
在Java中用问号来指代泛型中不确定类型的定义(如List<?>)。Scala用下划线来代替它,例:
scala> def testPrint(l: List[_]) = {
| list.foreach(x => println(x))
| }
testPrint: (l: List[_])Unit
得益于Scala模式匹配(pattern matching)的强大,下划线在模式匹配中的用途非常灵活。它除了可以用来代替Java switch-case表达方式中的default之外,还可以占位表示其他元素甚至类型。例:
str match {
case 1 => "one"
case 2 => "two"
// 如同Java中的default
case _ => "anything other than one and two"
}
expr match {
// 以1开头,且长度为3的List
case List(1,_,_) => "a list with three element and the first element is 1"
// 长度大于等于0的List
case List(_*) => "a list with zero or more elements"
// 键和值类型都为任意类型的Map
case Map[_,_] => "matches a map with any key type and any value type"
case _ =>
}
下划线可以实现Java import语句中星号的作用,但功能更强大一些。利用它还能导入时做重命名,以及忽略某些类。例:
// import java.util.concurrent.*
scala> import java.util.concurrent._
import java.util.concurrent._
// import java.util.*,并将ArrayList重命名为al
scala> import java.util.{ArrayList => al, _}
import java.util.{ArrayList=>al, _}
// import java.util.*,但不导入Timer类
scala> import java.util.{Timer => _, _}
import java.util.{Timer=>_, _}
// import static java.lang.Math.*
scala> import java.lang.Math._
import java.lang.Math._
用下划线可以自动在变量声明时,将其赋予默认的初始值。例:
scala> var name : String = _
name: String = null
scala> var count : Int = _
count: Int = 0
scala> var avg : Double = _
avg: Double = 0.0
下划线后面跟上数字k,可以当作索引表示元组中的第k个元素。当要忽略元组中的某个值时,也可以用下划线代替它。例:
scala> val tuple = ("LMagics", 173.5, Seq(22,66,88))
tuple: (String, Double, Seq[Int]) = (LMagics,173.5,List(22, 66, 88))
scala> tuple._1
res1: String = LMagics
scala> tuple._2
res2: Double = 173.5
scala> tuple._3
res3: Seq[Int] = List(22, 66, 88)
scala> val (first, _, third) = tuple
first: String = LMagics
third: Seq[Int] = List(22, 66, 88)
```scala
### 声明setter方法
Scala中没有显式的setter方法,将getter方法的命名后加上一个下划线,就可以当作setter方法使用。例:
```scala
scala> class Test {
| private var pCount = 0
| def count = pCount // getter
| def count_= (c : Int) = { // setter
| require(c > 0)
| pCount = c
| }
| }
defined class Test
scala> val test = new Test()
test: Test = Test@27d5a580
scala> test.count = 7
test.count: Int = 7
下划线与星号连用,可以将序列转化为变长参数的序列,方便调用变长参数的方法。例:
scala> def sum(args: Int*) = {
| var result = 0
| for (arg <- args) result += arg
| result
| }
sum: (args: Int*)Int
scala> val sum1 = sum(7,8,9,10,11,12,13,14)
sum1: Int = 84
// 如果只写7 to 14,会报错
scala> val sum2 = sum(7 to 14: _*)
sum2: Int = 84
我们都知道Scala中方法与函数的不同点。其中最显著的不同就在于,函数在Scala中是一种对象实例,因此它可以赋值给变量,也可以作为参数。如果方法在赋值时直接写名称的话,编译器会认为是对方法的调用,因此会报没有参数列表的错误。在方法名称后加一个下划线,会将其转化为偏应用函数(partially applied function),就能直接赋值了。例:
scala> def twoSum(a: Int, b: Int) = a + b
twoSum: (a: Int, b: Int)Int
scala> val twoSumFunc = twoSum _
twoSumFunc: (Int, Int) => Int = <function2>
scala> list foreach println _
5
3
7
9
1
既然偏应用函数就是不提供或者只提供部分参数调用时返回的函数,我们也可以用下划线来代替那些不提供的参数。例:
scala> def threeMult(a: Int, b: Int, c:Int) = a * b * c
threeMult: (a: Int, b: Int, c: Int)Int
scala> val parThreeMult = (8, _: Int, 42)
parThreeMult: Int => (Int, Int, Int) = <function1>
What are all the uses of an underscore in Scala?
import scala._ // Wild card -- all of Scala is imported
import scala.{ Predef => _, _ } // Exception, everything except Predef
def f[M[_]] // Higher kinded type parameter
def f(m: M[_]) // Existential type
_ + _ // Anonymous function placeholder parameter
m _ // Eta expansion of method into method value
m(_) // Partial function application
_ => 5 // Discarded parameter
case _ => // Wild card pattern -- matches anything
val (a, _) = (1, 2) // same thing
for (_ <- 1 to 10) // same thing
f(xs: _*) // Sequence xs is passed as multiple parameters to f(ys: T*)
case Seq(xs @ _*) // Identifier xs is bound to the whole matched sequence
var i: Int = _ // Initialization to the default value
def abc_<>! // An underscore must separate alphanumerics from symbols on identifiers
t._2 // Part of a method name, such as tuple getters
1_000_000 // Numeric literal separator (Scala 2.13+)
https://www.jianshu.com/p/d271afce8c71