I’m migrating code that used to use Scala 2 reflection.
I’m trying to use scala-reflection library.
I need to get the value of a field of a given name of a case class.
Example:
$ scala-cli -j system -S 3.4.2 --dep co.blocke::scala-reflection:2.0.8
scala> import co.blocke.scala_reflection.RType
scala> case class A(a: Int, b: String)
// defined case class A
scala> import co.blocke.scala_reflection.rtypes.ScalaClassRType
scala> val t = RType.of[A].asInstanceOf[ScalaClassRType[A]]
val t: co.blocke.scala_reflection.rtypes.ScalaClassRType[A] = ScalaClassRType(rs$line$2$A,rs$line$2$.A,List(),List(),List(),List(ScalaFieldInfo(0,a,IntRType(),Map(),None,None,false), ScalaFieldInfo(1,b,StringRType(),Map(),None,None,false)),Map(),List(java.lang.Object, scala.Product, java.io.Serializable),false,false,true,false,Map(),List(),List(),false)
scala> t.fields.find(_.name == "a")
val res2: Option[co.blocke.scala_reflection.rtypes.FieldInfo] = Some(ScalaFieldInfo(0,a,IntRType(),Map(),None,None,false))
scala> val c = A(3,"hola")
val c: A = A(3,hola)
In this example, how can I get the value of field a
by reflection on instance c
.
If it is easier, I could consider using other libraries like izumi-reflect.