Pio*_*lka 7 scala pattern-matching scala-2.8 scala-collections
在第410页的"Scala编程,第二版"中,您可以找到具有以下方法的类Simulation:
private def next() {
(agenda: @unchecked) match {
case item :: rest =>
agenda = rest
curtime = item.time
item.action()
}
}
Run Code Online (Sandbox Code Playgroud)
我很好奇为什么Odersky用模式匹配实现了这个,而不仅仅是这样:
private def next() {
val item = agenda.head
agenda = agenda.tail
curtime = item.time
item.action()
}
Run Code Online (Sandbox Code Playgroud)
模式匹配是否如此高效以至于根本没有关系?或者它只是不是那么完美的例子?
通常我会按你的方式写它.(即使模式匹配非常有效,它也不如头/尾有效.)如果使用模式匹配,则使用模式匹配
MatchException
而不是一个NoSuchElementException