Scala 中的魔法偏函数
我不认为这段代码应该有效,但它确实有效(在 Scala 2.10 中):
I don't think this code should work, but it does (in 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
它是如何工作的?我希望在 res0
中抛出 MatchError
.
How does it work? I would expect a MatchError
to be thrown inside res0
.
Scala 语言规范似乎没有明确说明应该如何解释 res0
.
The Scala language specification does not seem to explicitly document how res0
should be interpreted.
诀窍在于编译器不会将您的定义解释为转换为偏函数的全函数——它实际上首先创建了偏函数.您可以通过注意 res0.isDefinedAt(1) == false
来验证.
The trick is that the compiler is not interpreting your definition as a total function converted to a partial function -- it's actually creating a partial function in the first place. You can verify by noting that res0.isDefinedAt(1) == false
.
如果你真的把一个全函数转换成一个偏函数,你会得到你期望的行为:
If you actually convert a total function to a partial function, you will get the behavior you expected:
scala> PartialFunction((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"
| })
res0: PartialFunction[Int,String] = <function1>
scala> res0 orElse ({ case i => i.toString }: PartialFunction[Int, String])
res1: PartialFunction[Int,String] = <function1>
scala> res1(1)
scala.MatchError: 1 (of class java.lang.String)
// ...
在此示例中,PartialFunction.apply
将其参数视为一个全函数,因此有关其定义位置的任何信息都将丢失.
In this example, PartialFunction.apply
treats its argument as a total function, so any information about where it's defined is lost.