Search code examples
scalagenericsannotationsshapeless

Shapeless cannot provide implicit instance of Annotations.Aux


I'm trying to simplify annotations extracting from case classes, but I still want to stay as generic as possible. So, I've created a simple type class that does nothing useful (for now), but it accepts two type params:

trait Show2[+A, -T] {
  def show: Unit
}

And I have a simple implicit function to derive instances of this type class:

implicit def genericEncoder[A, T, ARepr <: HList](
  implicit annotations: Annotations.Aux[A, T, ARepr]
): Show2[A, T] = new Show2[A, T] {
  override def show: Unit = println(annotations())
}

Basically, it's some kind of wrapper around Annotations.Aux.

To test I use a data class and annotation class itself:

case class ann(v: Int) extends StaticAnnotation
case class C(@ann(1) s: String, @ann(2) i: Int)

So, I'm expecting that something will be printed out after

val encoder = implicitly[Show2[ann, C]]
encoder.show

But I'm getting could not find implicit value for parameter e: Show2[Main2.ann,Main2.C]. When I use concrete type ann instead of generic A in genericEncoder it works, but it's not what I need.

Any thoughts, what am I doing wrong here?

scastie snippet


Solution

  • it seems that it doesn't work because of variances in the Show2 typeclass (type parameters in Annotations.Aux are invariant)