Data class copy visibility modifier


Is there any way to change the visibility modifier of a copy method or disable its creation somehow? For instance, I would like to check invariants before creating a data class but the copy method might allow invalid instances to be created by copying the original object and changing its attributes.

Checking the docs I didn’t find a way of doing it:

Thanks in advance.

seems like it’s not really a data class if you want to do this kind of validity logic inside of it or disable the copy method. Just make a normal class instead. If there’s some data class-like things you still want, you can implement them yourself (like the componentN() stuff)

1 Like

Don’t forget that you can put validation checks in an init { } block inside the data class; that’ll be called whenever an instance is created, whether by calling its constructor directly, or via the copy() method.


There is a 3rd party compiler plugin that removes copy method. I’ve never used it so I don’t know if there are any issues.