聊聊 scala 的模式匹配
一. scala 模式匹配(pattern matching) pattern matching 可以说是 scala 中十分强大的一个语言特性,当然这不是 scala 独有的,但这不妨碍它成为 scala 的语言的一大利器。 scala 的 pattern matching 是类似这样的, e match { case Pattern1 => do Something case Pattern2 if-clause => do others ... } 其中,变量 e 后面接一个 match 以及一个代码块,其中每个 case 对应一种可能回匹配的类型,如果匹配成功则执行 => 后面的代码。 我们可以用一个具体一些的例子来看看模式匹配是怎么工作的: case class Player(name: String, score: Int) def printMessage(player: Player) = player match { case Player(_, score) if score > 100000 => println("Get a job...

