Scala Programming Language
  1. Scala Programming Language
  2. SI-6846

"polymorphic expression cannot be instantiated to expected type": foo: List[Int] inferred, foo: ListInt not.

    Details

      Description

      ticket/6745-3 ~/code/scala scalac210 sandbox/test.scala
      sandbox/test.scala:7: error: polymorphic expression cannot be instantiated to expected type;
       found   : [M[_], A]Test.Arb[M[A]]
       required: Test.Arb[Test.ListInt]
        foo: Arb[ListInt]
        ^
      one error found
      ticket/6745-3 ~/code/scala scalac29 sandbox/test.scala
      ticket/6745-3 ~/code/scala cat !$
      cat sandbox/test.scala
      object Test {
        class Arb[_]
        implicit def foo[M[_], A]: Arb[M[A]] = null
        foo: Arb[List[Int]]
      
        type ListInt = List[Int]
        foo: Arb[ListInt]
      }
      

        Issue Links

          Activity

          Hide
          Paul Phillips added a comment -

          Regressed in r25322 aka 658ba1b4e6.

          Show
          Paul Phillips added a comment - Regressed in r25322 aka 658ba1b4e6.
          Hide
          Paul Phillips added a comment -

          To make up for this, I threw in this one:

          object Test2 {
            import scala.collection.immutable.List
          
            class Carb[+_]
            implicit def bar[M[_], A]: Carb[M[A]] = null
          
            type ListInt = List[Int]
          
            val x: List[Int] = List(1)
            val y: ListInt = List(1)
          
            type ListSingletonX = x.type
            type ListSingletonY = y.type
          
            bar: Carb[List[Int]]
            bar: Carb[ListInt]
            bar: Carb[ListSingletonX]
            bar: Carb[ListSingletonY]
          }
          

          Compiles with my imminent pull request, in 2.9 did this:

          scalac29 -d /tmp test/files/pos/t6846.scala 
          test/files/pos/t6846.scala:25: error: polymorphic expression cannot be instantiated to expected type;
           found   : [M[_], A]Test2.Carb[M[A]]
           required: Test2.Carb[Test2.ListSingletonX]
            bar: Carb[ListSingletonX]
            ^
          test/files/pos/t6846.scala:26: error: polymorphic expression cannot be instantiated to expected type;
           found   : [M[_], A]Test2.Carb[M[A]]
           required: Test2.Carb[Test2.ListSingletonY]
            bar: Carb[ListSingletonY]
            ^
          two errors found
          
          Show
          Paul Phillips added a comment - To make up for this, I threw in this one: object Test2 { import scala.collection.immutable.List class Carb[+_] implicit def bar[M[_], A]: Carb[M[A]] = null type ListInt = List[Int] val x: List[Int] = List(1) val y: ListInt = List(1) type ListSingletonX = x.type type ListSingletonY = y.type bar: Carb[List[Int]] bar: Carb[ListInt] bar: Carb[ListSingletonX] bar: Carb[ListSingletonY] } Compiles with my imminent pull request, in 2.9 did this: scalac29 -d /tmp test/files/pos/t6846.scala test/files/pos/t6846.scala:25: error: polymorphic expression cannot be instantiated to expected type; found : [M[_], A]Test2.Carb[M[A]] required: Test2.Carb[Test2.ListSingletonX] bar: Carb[ListSingletonX] ^ test/files/pos/t6846.scala:26: error: polymorphic expression cannot be instantiated to expected type; found : [M[_], A]Test2.Carb[M[A]] required: Test2.Carb[Test2.ListSingletonY] bar: Carb[ListSingletonY] ^ two errors found
          Hide
          Paul Phillips added a comment -

          Oh, but I see now that type constructor inference is useless when combined variance - it appears to always choose Any/Nothing for the type argument if it can. Oh, in that case the invariant version is actually failing in the correct way. The 2.9 version also fails, but for the wrong reason.

          test/files/pos/t6846.scala:25: error: type mismatch;
           found   : Test2.Carb[List[Int]]
           required: Test2.Carb[Test2.ListSingletonX]
          Note: List[Int] >: Test2.ListSingletonX, but class Carb is invariant in type _.
          You may wish to define _ as -_ instead. (SLS 4.5)
            bar: Carb[ListSingletonX]
            ^
          test/files/pos/t6846.scala:26: error: type mismatch;
           found   : Test2.Carb[List[Int]]
           required: Test2.Carb[Test2.ListSingletonY]
          Note: List[Int] >: Test2.ListSingletonY, but class Carb is invariant in type _.
          You may wish to define _ as -_ instead. (SLS 4.5)
            bar: Carb[ListSingletonY]
            ^
          two errors found
          
          Show
          Paul Phillips added a comment - Oh, but I see now that type constructor inference is useless when combined variance - it appears to always choose Any/Nothing for the type argument if it can. Oh, in that case the invariant version is actually failing in the correct way. The 2.9 version also fails, but for the wrong reason. test/files/pos/t6846.scala:25: error: type mismatch; found : Test2.Carb[List[Int]] required: Test2.Carb[Test2.ListSingletonX] Note: List[Int] >: Test2.ListSingletonX, but class Carb is invariant in type _. You may wish to define _ as -_ instead. (SLS 4.5) bar: Carb[ListSingletonX] ^ test/files/pos/t6846.scala:26: error: type mismatch; found : Test2.Carb[List[Int]] required: Test2.Carb[Test2.ListSingletonY] Note: List[Int] >: Test2.ListSingletonY, but class Carb is invariant in type _. You may wish to define _ as -_ instead. (SLS 4.5) bar: Carb[ListSingletonY] ^ two errors found
          Show
          Adriaan Moors added a comment - https://github.com/scala/scala/issues/1869

            People

            • Assignee:
              Paul Phillips
              Reporter:
              Jason Zaugg
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development