mercredi 15 février 2017

Obtaining the full info about fields of the case class in Scala

Consider the following class and method:

case class User(id: Long, name: String) {
  private var foo = "Foo" // shouldn't be printed
  val bar = "bar" // also shouldn't be printed
}
case class Message(id: Long, userId: Long, text: String)

def printInfo[E](o: E)(implicit tt: TypeTag[E]) = {

}

I want to make this method print the name, type and value for each of the field for any case class, i.e.

printInfo(User(1, "usr1")) // prints something like "(id, Long, 1), (name, String)"
printInfo(Message(1, 1, "Hello World")) // prints "(id, Long, 1), (userId, Long, 1), (text, String, "Hello World")"

Adding some custom annotations for fields is also considerable.





Aucun commentaire:

Enregistrer un commentaire