On this link: https://stackoverflow.com/a/4055850/82609
It explains that
case class Person(name: String, age: Int) {
override def productPrefix = "person: "
}
// prints "person: (Aaron,28)" instead of "Person(Aaron, 28)"
println(Person("Aaron", 28))
Is there a way to do something like mixing the case class with some trait do provide a better ToString than the default one?
I don't really like to not have the field names printed, and for large case classes it is sometimes hard to read the logs.
Is it possible to have an output like this?
Person(
name="Aaron",
age=28
)
How about overriding toString()
? You can do that even in a specific trait (or each time at the level of the case class and calling an object function).
trait CustomToString {
override def toString() = "do some reflection magic here"
}
case class Person(name: String, age: Int) extends CustomToString
println(Person("Belä", 222))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With