3
votes

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.

1

1 Answers

1
votes

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.