Skip to content

Instantly share code, notes, and snippets.

@cloudcalvin
Created August 16, 2015 19:06
Show Gist options
  • Select an option

  • Save cloudcalvin/a5d76090a8f780ce8fe3 to your computer and use it in GitHub Desktop.

Select an option

Save cloudcalvin/a5d76090a8f780ce8fe3 to your computer and use it in GitHub Desktop.
Print Scala class members fields with types
object Implicits {
implicit class CaseClassToString(c: AnyRef) {
def toStringWithFields: String = {
val fields = (Map[String, Any]() /: c.getClass.getDeclaredFields) { (a, f) =>
f.setAccessible(true)
a + (f.getName -> f.get(c))
}
s"${c.getClass.getName}(${fields.mkString(", ")})"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment