It is possible to create property extensions, and in the example quoted it should work because I believe that in the background it is not a property, to tell the truth or I don’t really like the name, because I think that property implies having a state field linked, which is not very the case. Then you can create a method getter and even a Setter depending on what you want to do and the usage syntax will be equal to that of a field, but it cannot have an associated state field, so the answer is yes to the behavior and not to the state.
You can add behaviors and even extra states to your own data class
also, but they will not be part of the basic class structure, for example a property that is not in the primary constructor does not enter the equals()
, or toString()
.
fun main(args: Array<String>) {
val pessoa = Person("João", "Silva")
pessoa.age = 47
println(pessoa.fullName)
println(pessoa.Firula())
println(pessoa.firula)
val pessoa2 = pessoa.copy()
println(pessoa2.age)
pessoa2.age = 40
println(pessoa2.age)
println(pessoa == pessoa2)
println(pessoa2)
}
data class Person(val firstName: String, val lastName: String) {
fun Firula(): String = "O nome dele é " + firstName + " " + lastName
val firula: String
get() = "O nome dele é " + firstName + " " + lastName
var age: Int = 0
override fun toString() = firstName + " " + lastName
}
val Person.fullName: String
get() = firstName + " " + lastName
Behold working in the ideone. And in the Kotlin Play. Also put on the Github for future reference.
Of course it is possible to extend with functions also that only has behavior.
Just note that the extension occurs statically, has nothing of inheritance in this extension. So they’re just facilitators, it doesn’t change the class structure, whether it’s a data class or a normal class.
Add new properties? How would you think about doing that? In itself? Isn’t just changing the font? Of course you may have problems if it has already been used in some code, as in any class. Creating a new one based on this cannot. What I’m trying to see is if it gives to put non-standard behaviors in it.
– Maniero
@bigown Did not think, the premise was the language allow. And through Extension Properties is possible?
– ramaral
I got it, I gotta find out.
– Maniero
@ramaral, you can also create properties that originate from others, either by delegation or even at construction time.
– Wakim
@Wakim You can illustrate how?
– ramaral
This must be the 98th time that this very same question reaches the front page with title and content changed. What’s going on? Needless to say!
– user86792
@Can Tonymontana explain what you’re getting at? The title has never been changed and the two changes were minimal. The first, to focus the question only on the addition of properties (POJO by definition has no behavior), the second, to make it coherent with the code (with
val
is not generated Setter).– ramaral
From what I see the right answer has not yet appeared hehe. What answers @ramaral’s question is that data class is a class like any other, with a few restrictions. All the others, including mine, don’t address this, talk about extensions and properties generated about others. If so, it would be better to have an answer with that content.
– Wakim
@Wakim You are right. I think the only restriction is that it cannot be inherited. As far as I can tell, it’s possible to pick up any class you already have keyword data and everything works, receiving "free" these automatic methods. I may be wrong, and I’m sorry if I am, but what happened was that the answers were originally a little hasty and somewhat lacking in knowledge. They went after me for mentioning the possibility of using Extension Properties, however the bigown already approaches it. I suggest you edit your answer accordingly.
– ramaral
@Wakim Note that the bigown refers, in a commenting, that "this is being excellent learning"
– ramaral