Scala Value类汇编的基本类型失败,使用部分功能参数法



说,我定义了一个值类如下

package object p {
  class ValueClass[T](val o: Option[T]) extends AnyVal {
    def foo: Option[T] =
      o collect {
        case t => t
      }
  }
}

汇编失败了:

overriding method applyOrElse in trait PartialFunction of type [A1 <: T, B1 >: T](x: A1, default: A1 => B1)B1;
  method applyOrElse has incompatible type
       o collect {
                 ^

错误消息对我来说似乎并不有意义,好像我由map替换collect(需要函数而不是部分函数)或类不扩展AnyVal,代码片段将编译。

任何人都可以解释背后的原因或链接到提交的问题吗?

这是一个很好的。它至少在2.11.0-m8中起作用。

显然,

https://issues.scala-lang.org/browse/si-8011

确切匹配

https://issues.scala-lang.org/browse/si-8018

在2.10.x上,

scala> trait X[A] { def pf: PartialFunction[A,A] = { case a => a } }
defined trait X
scala> class X[A](val x: A) extends AnyVal { def pf: PartialFunction[A,A] = { case a => a } }
<console>:7: error: overriding method applyOrElse in trait PartialFunction of type [A1 <: A, B1 >: A](x: A1, default: A1 => B1)B1;
 method applyOrElse has incompatible type
       class X[A](val x: A) extends AnyVal { def pf: PartialFunction[A,A] = { case a => a } }
                                                                            ^

也许在2.10.3允许之后没有出现这个问题。

对于病态的好奇或闲置,以前有:

https://issues.scala-lang.org/browse/si-6482

https://issues.scala-lang.org/browse/si-7022(重复)

https://issues.scala-lang.org/browse/si-6187

好吧,正式验证实际上是一个很好的验证。这是后端口移植的提交:

commit ff9f60f420c090b6716c927ab0359b082f2299de
Author: Paul Phillips <paulp@improving.org>
Date:   Sat Oct 6 10:20:45 2012 -0700
    Fix for SI-6482, lost bounds in extension methods.
    That was a good one. How to create a new method with type
    parameters from multiple sources, herein.

更新显示2.11.0-m8做某事:

apm@mara:~/goof$ scalam
Welcome to Scala version 2.11.0-M8 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class X[A](val x: A) extends AnyVal { def pf: PartialFunction[A,A] = { case a => a } }
defined class X
scala> :pa -raw
// Entering paste mode (ctrl-D to finish)
package object p {
  class ValueClass[T](val o: Option[T]) extends AnyVal {
    def foo: Option[T] =
      o collect {
        case t => t
      }
  }
}
// Exiting paste mode, now interpreting.

scala> new p.ValueClass(Some(7)).foo
res0: Option[Int] = Some(7)

相关内容

  • 没有找到相关文章

最新更新