``` class Human(val name: String, age: Int) { ...
# announcements
l
Copy code
class Human(val name: String, age: Int) {
    var age = age
        private set
    
    fun birthday() {
        //party hard
        age++
    }
}
can this be simplified somehow or is it good as is?
p
If immutable class will work in your case, then you can write:
Copy code
data class Human(
    val name: String,
    val age: Int
)

fun Human.birthday() = copy(age = age + 1)
In this case
birthday
returns new instance instead of modifying existing one.
l
nah it needs to be mutable, i was wondering if its possible to set getters and setters for variables declared in the primary constructor (like
name
in my example)
d
Copy code
data class Human(
    val name: String,
    private var age: Int
) {
    fun birthday() {
        //party hard
        age++
    }
}
l
@dawidhyzy in your example the setter of
age
isn't private, is it?
d
next time post this type of question in #C0B8MA7FA
now it is
l
but now the getter is private too
d
then I think this is the best you can get:
Copy code
class Human(val name: String, age: Int) {
    var age = age
        private set
    
    fun birthday() {
        //party hard
        age++
    }
}