I am trying to convert my case class into a sequence containing a lens for each field. I've created the following simplified example to highlight the problem that I am having.
The following code will give a runtime error:
import shapeless._
case class Testing(field1: String, field2: Double)
val lenses = Seq(0,1).map(i => lens[Testing] >> i)
whereas the following does not:
import shapeless._
case class Testing(field1: String, field2: Double)
val lens1 = lens[Testing] >> 0
val lens2 = lens[Testing] >> 1
val lenses = Seq(lens1, lens2)
The actual error reads "Expression i does not evaluate to a non-negative Int literal".
I feel like this error message is misleading since the code val lens3 = lens[Testing] >> 2 (i.e. accessing one field too many) would give the same error message.
Has anyone experienced behaviour like this in shapeless? And is there an easier way to extract element lenses for each field in my Case Class into a sequence (i.e. not like @lenses in monocle where you still need to access each lens using the field name)?
are implicitly transformed to
and this works but
or
val lenses = Seq(Nat._0,Nat._1).map(i => lens[Testing] >> i)doesn't.Seq(Nat._0,Nat._1)has typeSeq[Nat], soihas typeNat(rather than specificNat._0,Nat._1) and this is too rough.The following approach with constructing
HListof lenses (rather thanSeq) seems to work: