Reputation: 3362
I have been writing an app where at first I declared the class signature as below
data class MClass(val id: Int = 0,val descr: String, val timestamp: Long)
Now a need was created where I must have a custom getter for a field above. How can I write this custom getter? I know that If otherwise I could write something like
data class(){
val id=0
val descr = ""
get() = descr + "append smth"
val timestamp:Long = 0
}
Upvotes: 2
Views: 4311
Reputation: 5251
You could do something like below:
data class MClass(val id: Int = 0, private val descr: String, val timestamp: Long) {
val description: String
get() = descr + "append smth"
}
Upvotes: 9
Reputation: 474
You can make it like that:
data class MClass(val id: Int = 0, private val _descr: String, val timestamp: Long) {
val descr: String
get() = _descr + "append something"
}
Upvotes: 1