bug icon indicating copy to clipboard operation
bug copied to clipboard

Inference failing with default argument and type parameter

Open scabug opened this issue 12 years ago • 2 comments

object Test {

  class Foo[A, B] {
    def bar[T](v: A => A = (identity _)) = Unit
    // 2.10 => OK
    // 2.11 => OK

    def bar[T](v: A => T = (identity[A] _)) = Unit
    // 2.10 => OK
    // 2.11 => OK

    def bar[T](v: A => T = (identity _)) = Unit
    // 2.10 => "polymorphic expression cannot be" and then NPE in compiler
    // 2.11 => "polymorphic expression cannot be"

    // So what is the inference fail here??
  }

  new Foo[String, Int]().bar()
}

'def bar[T](v: A => T = (identity _))', on 2.10 an NPE is throw by the compiler, on 2.11 just a: [error] /home/alois/oss/scala-testing/src/test/scala/FunctionalSpec.scala:18: polymorphic expression cannot be instantiated to expected type; [error] found : [T]Nothing => Nothing [error] required: String => ?

Probably tricky but compiler should be able to infer this isn't it?

scabug avatar Feb 07 '13 15:02 scabug

Imported From: https://issues.scala-lang.org/browse/SI-7095?orig=1 Reporter: Alois Cochard (aloiscochard) Affected Versions: 2.11.0

scabug avatar Feb 07 '13 15:02 scabug

t7095.scala:4: error: `Unit` companion object is not allowed in source; instead, use `()` for the unit value
    def bar[T](v: A => A = (identity _)) = Unit
                                           ^

Same behavior. Maybe related https://github.com/scala/bug/issues/9970

som-snytt avatar Nov 24 '20 20:11 som-snytt