Anything I can do to exclude a particular field fr...
# announcements
u
Anything I can do to exclude a particular field from data class
.toString()
except overriding it? I have a LAZY collection in a data class, so it throws an exception on
.toString()
a
Is a data class appropriate for this use case then? Would .equals or .hashCode accessing that lazy collection cause similar problems?
n
data classes by default use primary constructor parameters for toString, hashCode, and equals. You could use
Copy code
fun foo() = (1..10).random()
data class D(val a: Int) {
    val b: Int by lazy { foo() }
}
val d1 = D(33)
println("d1 is $d1 and d1.b is ${d1.b}")
val d2 = D(33)
println("d2.b is ${d2.b} and d1 and d2 are equal: ${d1 == d2}")
which printed for me: d1 is D(a=33) and d1.b is 9 d2.b is 6 and d1 and d2 are equal: true