https://kotlinlang.org logo
#announcements
Title
# announcements
g

gerson

03/03/2017, 3:12 PM
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

mfulton26

03/03/2017, 3:44 PM
gerson:
data class Person(val name: String, parent: Person) { val parent: Person = parent }
r

robin

03/03/2017, 3:44 PM
That doesn't work, data classes are not allowed to have non-
val
and non-
var
parameters
m

mfulton26

03/03/2017, 3:45 PM
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

robin

03/03/2017, 3:47 PM
That should work, but that's getting quite complicated already... I think overwriting
toString
might be the easiest way.
m

mfulton26

03/03/2017, 3:48 PM
Yes, it may be. Or just include the property.
g

gerson

03/03/2017, 4:17 PM
@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!
4 Views