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

LowPriority and cachedImplicit incompatibility #720

Open
durban opened this issue May 11, 2017 · 1 comment
Open

LowPriority and cachedImplicit incompatibility #720

durban opened this issue May 11, 2017 · 1 comment
Labels

Comments

@durban
Copy link
Contributor

durban commented May 11, 2017

LowPriority and cachedImplicit seem to be incompatible:

package foo

import shapeless.{ cachedImplicit, Generic, LowPriority }

trait Foo[A]

object Foo {

  implicit def fooForOption[A]: Foo[Option[A]] =
    new Foo[Option[A]] {}

  implicit def fooFromGeneric[A, R](implicit A: Generic.Aux[A, R], lowPrio: LowPriority): Foo[A] =
    new Foo[A] {}
}

object Bar {

  final case class MyClass(a: Int, b: Int)
  object MyClass {
    implicit val fooInstance: Foo[MyClass] =
      cachedImplicit[Foo[MyClass]] // ERROR
  }

  implicitly[Foo[Option[Int]]]
}

The error is:

[error] .../Foo.scala:21: Could not find an implicit value of type foo.Foo[foo.Bar.MyClass] to cache
[error]       cachedImplicit[Foo[MyClass]]
[error]                     ^

The following workaround (i.e., doing it without LowPriority) seem to work (but not always possible):

package foo

import shapeless.{ cachedImplicit, Generic, LowPriority }

trait Foo2[A]

object Foo2 extends LowPrioFoo2 {
  implicit def fooForOption[A]: Foo2[Option[A]] =
    new Foo2[Option[A]] {}
}

trait LowPrioFoo2 {
  implicit def fooFromGeneric[A, R](implicit A: Generic.Aux[A, R]): Foo2[A] =
    new Foo2[A] {}
}

object Bar2 {

  final case class MyClass(a: Int, b: Int)
  object MyClass {
    implicit val fooInstance: Foo2[MyClass] =
      cachedImplicit[Foo2[MyClass]]
  }

  implicitly[Foo2[Option[Int]]]
}
@milessabin
Copy link
Owner

I'm afraid that all of these mechanisms are a bit Heath Robinsonish ... I'm hoping to address the prioritization issue directly in the compiler.

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

No branches or pull requests

3 participants