为什么 PartialFunction 上没有接受总计函数的 orElse 方法?

发布于 2024-12-09 14:48:46 字数 190 浏览 0 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

|煩躁 2024-12-16 14:48:46
  1. 因为通过提升部分功能来实现相同的效果是微不足道的

    partialFunc.lift(arg) getOrElse (totalFunc(arg))

  2. 因为 Scala 通常会尝试避免重载

  3. 因为没有人想到添加它,到目前为止可能还不需要它

  4. 因为添加到标准库中的每个方法都会产生不断增长的成本下游维护方面

  1. Because it's trivial to achieve the same by lifting the partial function

    partialFunc.lift(arg) getOrElse (totalFunc(arg))

  2. Because Scala, generally, tries to avoid overloading

  3. Because nobody thought to add it, and it's probably not been needed so far

  4. Because each and every method added to the standard library incurs an ever-growing cost in terms of downstream maintainence

最单纯的乌龟 2024-12-16 14:48:46

考虑一下,

scala> object O {
     |   def f(g: Int => Int) = g(1)
     |   def f(g: PartialFunction[Int, Int]) = g(2).toString
     | }
defined module O

scala> O f { _ * 1 }
res3: Int = 1

那么,现在如何链接部分函数呢?或者,换句话说,如果您描述的重载在库中,并且我写了这样的内容:

type PF = PartialFunction[Any, Int]
val pf1: PF = { case n: Int => n }
val pf2: PF = pf1 orElse { case x: String => x.length }
val pf3: PF = pf2 orElse { case d: Double => d.toInt }

由于类型不明确,我会在 pf2 上收到一条错误消息。相反,如果我写:

val pf2 = pf1 orElse ((_: Any) match { case x: String => x.length })
val pf3 = pf2 orElse ((_: Any) match { case d: Double => d.toInt })

那么我在 pf3 上收到错误,因为 pf2 将是 Function1

Consider,

scala> object O {
     |   def f(g: Int => Int) = g(1)
     |   def f(g: PartialFunction[Int, Int]) = g(2).toString
     | }
defined module O

scala> O f { _ * 1 }
res3: Int = 1

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:

type PF = PartialFunction[Any, Int]
val pf1: PF = { case n: Int => n }
val pf2: PF = pf1 orElse { case x: String => x.length }
val pf3: PF = pf2 orElse { case d: Double => d.toInt }

I'd get an error message on pf2 because of the ambiguity of the type. If, instead, I write:

val pf2 = pf1 orElse ((_: Any) match { case x: String => x.length })
val pf3 = pf2 orElse ((_: Any) match { case d: Double => d.toInt })

Then I get an error on pf3, because pf2 will be a Function1.

淡写薰衣草的香 2024-12-16 14:48:46

看来标准库中没有这些函数没有任何充分的理由,除了监督之外,或者也许没有人像在标准库中那样频繁地需要这些函数。

我已经定义了从 A => 的隐式转换; BPartialFunction[A, B] 似乎可以处理这种情况和其他类似的情况,并且不会导致不利影响。

scala> implicit def fToPf[A, B](f: A => B) = new PartialFunction[A, B] {
     |   def isDefinedAt(a: A) = true
     |   def apply(a: A) = f(a)
     | }
fToPf: [A, B](f: A => B)java.lang.Object with 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 to PartialFunction[A, B] which seems to take care of this and other similar cases, and doesn't lead to adverse effects.

scala> implicit def fToPf[A, B](f: A => B) = new PartialFunction[A, B] {
     |   def isDefinedAt(a: A) = true
     |   def apply(a: A) = f(a)
     | }
fToPf: [A, B](f: A => B)java.lang.Object with PartialFunction[A,B]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文