Skip to content

Implicits classes not working for type aliases of types with invariance/covariance (unhelpful compiler error) #12352

Open
@jbwheatley

Description

@jbwheatley

reproduction steps

using Scala 2.13.x,

type Foo[F[_], A, B] = Kleisli[F, A, B]

val foo: Foo[Option, Int, Int] = Kleisli { i => Option(i) }

implicit class FooOps[F[_], A, B](val foo: Foo[F, A, B]) {
  def bar(a: A): F[B] = foo.run(a)
}

foo.bar(1) //does not compile

or see scastie snippet here https://scastie.scala-lang.org/uIri16dwQWWatxSnNyTBVg

problem

The compiler fails with value bar is not a member of...:

I found 2 ways of removing this error, either defining the implicit class using the the type being aliased:

implicit class FooOps2[F[_], A, B](val foo: Kleisli[F, A, B]) extends AnyVal {
  def bar2(a: A): F[B] = foo.run(a)
}

foo.bar2(1) //compiles

or by including the contravariance of Kleisli's second type param in the type alias too:

type Foo[F[_], -A, B] = Kleisli[F, A, B] // `-` included on the `A`!

val foo: Foo[Option, Int, Int] = Kleisli { i => Option(i) }

implicit class FooOps[F[_], A, B](val foo: Foo[F, A, B]) {
  def bar(a: A): F[B] = foo.run(a)
}

foo.bar(1) //compiles

The compiler error was quite unhelpful in debugging this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions