为什么 PartialFunction 上没有接受总计函数的 orElse 方法?
为什么 PartialFunction[A, B]
类中没有具有以下签名的方法?
def orElse[A1 <: A, B1 >: B](that: A1 => B1): A1 => B1
缺乏这种方法背后是否有某种逻辑原因,或者仅仅是一个疏忽?
Why is there no method with following signature on class PartialFunction[A, B]
?
def orElse[A1 <: A, B1 >: B](that: A1 => B1): A1 => B1
Is there some logical reason behind the absence of this method, or was it a mere oversight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为通过提升部分功能来实现相同的效果是微不足道的
partialFunc.lift(arg) getOrElse (totalFunc(arg))
因为 Scala 通常会尝试避免重载
因为没有人想到添加它,到目前为止可能还不需要它
因为添加到标准库中的每个方法都会产生不断增长的成本下游维护方面
Because it's trivial to achieve the same by lifting the partial function
partialFunc.lift(arg) getOrElse (totalFunc(arg))
Because Scala, generally, tries to avoid overloading
Because nobody thought to add it, and it's probably not been needed so far
Because each and every method added to the standard library incurs an ever-growing cost in terms of downstream maintainence
考虑一下,
那么,现在如何链接部分函数呢?或者,换句话说,如果您描述的重载在库中,并且我写了这样的内容:
由于类型不明确,我会在
pf2
上收到一条错误消息。相反,如果我写:那么我在
pf3
上收到错误,因为pf2
将是Function1
。Consider,
So, how do you chain partial functions now? Or, in other words, if the overload you describe was in the library, and I wrote this:
I'd get an error message on
pf2
because of the ambiguity of the type. If, instead, I write:Then I get an error on
pf3
, becausepf2
will be aFunction1
.看来标准库中没有这些函数没有任何充分的理由,除了监督之外,或者也许没有人像在标准库中那样频繁地需要这些函数。
我已经定义了从
A => 的隐式转换; B
到PartialFunction[A, B]
似乎可以处理这种情况和其他类似的情况,并且不会导致不利影响。It appears there isn't any good reason for the absence of these functions in the standard library, other than oversight or maybe nobody needs these as often as to have them in standard library.
I have defined an implicit conversion from
A => B
toPartialFunction[A, B]
which seems to take care of this and other similar cases, and doesn't lead to adverse effects.