Skip to content

Implicit resolution with type aliases with phantom types doesn't work #10197

@scabug

Description

@scabug
import scala.language.higherKinds

final case class Getter[S, A](get: S => A)

final case class Wrap[F[_], A](value: F[A])

object Wrap {
  // Helper to defer specifying second argument to Wrap.
  //  Basically a type lambda specialized for Wrap.
  // Wr[F]#ap[A] =:= Wrap[F, A]
  type Wr[F[_]] = { type ap[A] = Wrap[F, A] }

  implicit def unwrapper[F[_], A]: Getter[Wrap[F, A], F[A]] =
    Getter(w => w.value)
}

object Test {
  import Wrap._

  type Foo[A] = List[A]
  type Bar[A] = String

  type WrapFoo1[A] = Wrap[Foo, A]
  type WrapBar1[A] = Wrap[Bar, A]

  implicitly[Getter[WrapFoo1[Int], Foo[Int]]] // OK
  implicitly[Getter[WrapBar1[Int], Bar[Int]]] // OK

  type WrapFoo2[A] = Wr[Foo]#ap[A]
  type WrapBar2[A] = Wr[Bar]#ap[A]

  // here's evidence that the new types are the same as the old ones
  implicitly[WrapFoo2[Int] =:= WrapFoo1[Int]]
  implicitly[WrapBar2[Int] =:= WrapBar1[Int]]

  // but implicit resolution doesn't work as before
  implicitly[Getter[WrapFoo2[Int], Foo[Int]]] // OK
  implicitly[Getter[WrapBar2[Int], Bar[Int]]] // error: could not find implicit value for parameter
                                              //   e: Getter[Test.WrapBar2[Int],Test.Bar[Int]]
}

Note that it is the combination of type alias with phantom type parameter (type Bar[A] = String) and type lambda (Wr[Bar]#ap) that is problematic. Each of the three simpler cases works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions