先看个正常的写法:
scala> val x = 1
x: Int = 1
体会一下元组的写法:
scala> val (x,y,z)=(1,2,3)
x: Int = 1
y: Int = 2
z: Int = 3
再来体会一个Array的匹配:
scala> val Array(x,y,_)=Array(1,2,3)
x: Int = 1
y: Int = 2
很好理解嘛!调用了Array伴生对象的unapplySeq(arr)嘛!
再来看个崩溃一点的:
scala> val pattern = "([0-9]+) ([a-z]+)".r
pattern: scala.util.matching.Regex = ([0-9]+) ([a-z]+)
scala> val pattern(x,y)="123 abc"
x: String = 123
y: String = abc
这个调用了Pattern类的unapplySeq()