【发布时间】:2013-11-14 08:40:20
【问题描述】:
我不认为这段代码应该有效,但它确实有效(在 Scala 2.10 中):
scala> ((i: Int) => i.toString match {
| case s if s.length == 2 => "A two digit number"
| case s if s.length == 3 => "A three digit number"
| }): PartialFunction[Int,String]
res0: PartialFunction[Int,String] = <function1>
// other interactions omitted
scala> res0.orElse(PartialFunction((i: Int) => i.toString))
res5: PartialFunction[Int,String] = <function1>
scala> res5(1)
res6: String = 1
它是如何工作的?我希望MatchError 被扔进res0。
Scala 语言规范似乎没有明确说明应该如何解释 res0。
【问题讨论】:
标签: scala partialfunction