Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala value class compilation fails for base type with partial-function-parameter method

Say, I defined a value class as follows

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

The compilation failed with the message:

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 {
                 ^

The error message seems not so meaningful to me as if I replace collect by map (which requires a function instead of a partial function) or the class does not extend AnyVal, the code snippet will compile.

Could anyone please explain the reason behind or link to a submitted issue?

like image 506
user3281360 Avatar asked Feb 06 '14 20:02

user3281360


1 Answers

That's a good one. It works in 2.11.0-M8, at least.

Apparently,

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

with the exact match

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

On 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 } }
                                                                            ^

Maybe this issue didn't come up after 2.10.3 allowed it.

For the morbidly curious, or idle, there were previously:

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

https://issues.scala-lang.org/browse/SI-7022 (duplicate)

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

OK, official validation that it was in fact a good one. This is the commit from which the back port was ported:

commit ff9f60f420c090b6716c927ab0359b082f2299de
Author: Paul Phillips <[email protected]>
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.

Update showing that 2.11.0-M8 does something:

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)
like image 171
som-snytt Avatar answered Nov 06 '22 13:11

som-snytt