在Scala中使用模式匹配与非常简单的情况有什么意义吗?
发布时间:2020-12-16 19:18:28 所属栏目:安全 来源:网络整理
导读:在“ Scala编程,第二版”(第410页)中,您可以找到具有以下方法的类Simulation: private def next() { (agenda: @unchecked) match { case item :: rest = agenda = rest curtime = item.time item.action() }} 我很好奇为什么Odersky用模式匹配实现了这个,而
在“
Scala编程,第二版”(第410页)中,您可以找到具有以下方法的类Simulation:
private def next() { (agenda: @unchecked) match { case item :: rest => agenda = rest curtime = item.time item.action() } } 我很好奇为什么Odersky用模式匹配实现了这个,而不仅仅是这样: private def next() { val item = agenda.head agenda = agenda.tail curtime = item.time item.action() } 模式匹配是否如此高效以至于根本没有关系? 解决方法
通常我会按你的方式写它. (即使模式匹配非常有效,它也不如头/尾有效.)如果使用模式匹配,则使用模式匹配
>你想练习模式匹配>您需要MatchException而不是NoSuchElementException>你以后会填写其他案件. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |