Reputation: 11450
In Kotlin, is it possible to have a property, that's declared in the body of a data class, be included in the default toString()
result?
data class A(val b:Int = 0) {
val c: Int = 0
}
println(A())
Prints: A(b=0)
Desired: A(b=0, c=0)
Upvotes: 3
Views: 986
Reputation: 89578
Not in the generated toString
, that will only have the properties declared in the primary constructor (as described in the docs). However, you can always override toString
yourself if you want to:
data class A(val b:Int = 0) {
val c: Int = 0
override fun toString(): String {
return "A(b=$b, c=$c)"
}
}
Upvotes: 5