Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference can't infer covariant higher-kinder type #10188

Closed
scabug opened this issue Feb 13, 2017 · 2 comments
Closed

Type inference can't infer covariant higher-kinder type #10188

scabug opened this issue Feb 13, 2017 · 2 comments

Comments

@scabug
Copy link

scabug commented Feb 13, 2017

    class Case2 {
      trait Container[+A] {
        def addAll[B >: A, T2 <: Container[B]](that: T2): Boolean
      }

      def t1: Container[Int] = ???
      def t2: Container[Any] = ???

      // Works
      t1.addAll[Any, Container[Any]](t2)

      // Errors:
      //* type mismatch; found : Case2.this.Container[Any] required: T2
      //* inferred type arguments [Int,Case2.this.Container[Any]] do not conform to method addAll's type parameter bounds [B >: Int,T2 <: Case2.this.Container[B]]
      t1.addAll(t2)
    }

Why can't least common supertype be inferred?

@scabug
Copy link
Author

scabug commented Feb 13, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10188?orig=1
Reporter: Alexander Abdugafarov (frozenspider)
Affected Versions: 2.11.8, 2.12.1

@scabug
Copy link
Author

scabug commented Feb 15, 2017

@adriaanm said:
Type inference does not support bounds that refer to other type parameters. Why do you need T2? This works:

    def addAll[B >: A](that: Container[B]): Boolean

If you have to be able to specify the container, you could try:

    def addAll[B >: A, C[x] <: Container[x]](that: C[B]): Boolean

The inferred types then look like t1.addAll[Any, Container](t2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant