Hi guys. Is there an easy way to exclude a propert...
# announcements
g
Hi guys. Is there an easy way to exclude a property from being used in generated toString() in data class (without implementing my own toString())?
m
gerson:
data class Person(val name: String, parent: Person) { val parent: Person = parent }
r
That doesn't work, data classes are not allowed to have non-
val
and non-
var
parameters
m
oh yeah; I forgot; I suppose
data class Person(val name: String) { var parent: Person }
and then you could add a secondary constructor if you wanted to pass
parent
in.
And if you want
parent
to be a
val
then you would need to create a private
var
backing a public
val
.
r
That should work, but that's getting quite complicated already... I think overwriting
toString
might be the easiest way.
m
Yes, it may be. Or just include the property.
g
@robin You're right, that's getting quite complicated. I think I'll go with writing my own
toString
.
@mfulton26 Thanks for the suggestion, anyway!