shapeless implicit val LabelledGeneric cannot found in method where case class is defined

StackOverflow https://stackoverflow.com/questions/23465591

  •  15-07-2023
  •  | 
  •  

Question

I just find an interesting issue with shapeless lens, here is the code:

case class Person(name : String, age : Int)
val nameLens     = lens[Person] >> 'name

(1): If I define it in the object which extends App, it is working.

object mainLensSpec extends App {
   import shapeless._
   case class Person(name : String, age : Int)
   val nameLens     = lens[Person] >> 'name
}

(2). If I define in main method, it is not working.

object mainLensSpec {
def main(args: Array[String]) {
    import shapeless._
    case class Person(name : String, age : Int)
    val nameLens     = lens[Person] >> 'name
}

The error message: could not find implicit value for evidence parameter of type shapeless.LabelledGeneric[Person]{type Repr = Out0} val nameLens = lens[Person] >> 'name

However, if I move the definition case class Person out of method, it is working again !

  object mainLensSpec {
    import shapeless._
    case class Person(name : String, age : Int)

    def main(args: Array[String]) {
        val nameLens     = lens[Person] >> 'name
    }

My question is that

(1). why implicit value is not in the scope if case class in defined in the same method with lens.

(2) when shapeless is going to create LabelledGeneric[Person] and inject into >> method defined in Lens.

Many many many thanks in advance, lens is powerful and little confusing to me.

Was it helpful?

Solution

I'm fairly sure that this is a Scala compiler bug. I can't find an exactly match in the bug database for the problem you're seeing just at the moment, but it feels like it's probably a relative of SI-4225.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top