Is it possible to hide fields from the `toString` ...
# announcements
j
Is it possible to hide fields from the
toString
function on data classes? Can be with annotation or anything. I want to achieve something like this: https://pl.kotl.in/Ld0EBnHM1
n
just override it. that's what we do for a class that stores credentials.
j
v
Or don't have it in the primary constructor, that would also leave it out from
copy
for example if that is what you want:
Copy code
data class MyData private constructor(
	val value1: String,
    val value3: String
) {
    private lateinit var value2: String

    constructor(
	    value1: String,
        value2: String,
        value3: String
    ) : this(value1, value3) {
        this.value2 = value2
    }
}
n
this example makes me wonder why you are restricted to property parameters for dataclasses
j
I'm trying to make a library. So I want the developer writing the data classes to have it as minimal as possible. This Sekret Library looks really interesting. :)
n
The example above seems like a more complex way of writing:
Copy code
data class MyData constructor(
	val value1: String,
    value2: String,
    val value3: String
) {
    val value2 = value2
}
which Kotlin doesn't actually allow
not sure why Kotlin couldn't allow this
n
I think data classes have a lot of restrictions to keep them simple. like I wouldn't know what would be included in the copy/toString methods of that
n
How wouldn't you know? My example has nothing else that could possibly be part of copy
It adds an argument to the primary constructor, but no state
n
maybe I'm uniquely bad at reading constructors /s