Suppose I have a string with a field path of a Scala case class, e.g.
case class A1(x: Int)
case class A(a1: A1)
val x = "a1.x" // field path of "x" in "A"
I use this field path for runtime reflection. The problem is that these classes A and A1 may change and then the field path becomes invalid.
case class A1(x1: Int)
case class A(a1: A1)
val x = "a1.x" // invalid path in "A"
Now I want to validate the field path in compile time like this:
case class A1(x1: Int)
case class A(a1: A1)
val x = FieldPath[A]("a1.x") // compiler error
What is the best way to do it with Scala 2 ? I guess it's doable using Scala 2 macros but I don't know how to do that.
What you want is actually close to lenses
https://www.optics.dev/Monocle/docs/focus
https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0#boilerplate-free-lenses-for-arbitrary-case-classes
Maybe lenses would be enough for you. If you really want to validate strings like
"a1.x"you can write a macro reusing Monocle or Shapeless functionality