Skip to content

Cannot widen a HKT parameter with covariant type parameters #11789

@neko-kai

Description

@neko-kai

Example:

case class Holder[F[+_, +_]](v: F[Int, Int]) {
  def asWiden[F2[+x, +y] >: F[x, y]]: F2[Int, Int] = v
}

object example extends App {
  println(Holder[Right](Right(5)).asWiden[Either])
}

Produces error:

Error:(4, 16) covariant type x occurs in contravariant position in type [+x, +y] >: F[x,y] of type F2
  def asWiden[F2[+x, +y] >: F[x, y]]: F2[Int, Int] = v
              ^

Expected it to succeed, as the expression does not appear unsound!
Writing it as F2[+x, +y] >: F[_, _] passes the variance checker, but the expression using the function fails to typecheck:

Error:(7, 99) type arguments [Either] do not conform to method asWiden's type parameter bounds [F2[+x, +y] >: Right[_, _]]
def res0 = Holder[Right](Right(5)).asWiden[Either]                                                              ^

Scala version 2.13.1

Metadata

Metadata

Assignees

Labels

fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)has PR

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions