Using delegated properties in data classes


#1

Currently it’s not possible to use delegated properties in the primary constructor of data classes or any class for that matter.

e.g.

data class User(val name: String, var password: String by PasswordChecker())

Has this possibility been considered? It would be great if one can still benefit from the automatically derived features (equals/hashcode/tostring/copy/…) without sacrificing the power of delegation.


#2

I don’t really understand how this would work. I guess PasswordChecker supplies a getter and a setter. And the constructor should than call the setter of the PasswordChecker? But what if the delegate only supplies a getter. How is the value to be set than?
Pls correct me if I’m wrong.


#3

The compiler should raise an error in the case of a read-only property delegate, just like when you would apply a read-only property in the currently supported way:

data class User(val name: String) {
  var password: String by PasswordChecker()
}