Well, given the two questions I attached to the question, here is what I used:
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(", ")})" } } } case class PriceMove(price: Double, delta: Double) object Test extends App { import Implicits._ println(PriceMove(1.23, 2.56).toStringWithFields) }
This gives:
PriceMove(price -> 1.23, delta -> 2.56)
serejja
source share