What are the reason why we use or explicitly use get() and set() in Kotlin? I have a model that is throwing an error when I remove explicit get() in its variable declaration.
data class SampleDomain(
val publishTime: String = ""
) {
// Removing get() here, publishTime becomes empty
val formattedDate: String
get() = LocalDate.parse(publishTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME).format(
DateTimeFormatter.ofPattern("MMM. dd, yyyy")
)
}
get() and set() is how we define getters and setters in Kotlin. So simply answering why do we use them is because they are required to define a getter/setter.
If you mean the difference between the following definitions:
val formattedDate: String = acquireDate()
val formattedDate: String get() = acquireDate()
Then get() is not here just to be more explicit. These two code fragments do much different things. The first acquires the date during the object initialization, stores it inside a field and the getter returns this stored value. The second defines a custom getter, but the value is not stored anywhere - the date is acquired again and again every time the getter is invoked.
See the documentation for more info: https://kotlinlang.org/docs/properties.html#getters-and-setters
As broot says, adding get() changes the meaning very significantly.
Without the get(), you're declaring a property with an initial value. For example:
class SomeClass {
var prop = initialisingFunction()
}
That's roughly equivalent to:
class SomeClass {
var prop: Int
init {
prop = initialisingFunction()
}
}
So initialisingFunction() is called only once, during construction, and the class stores its value in a backing field after that. If the property is a var, as here, then the value can be changed after that.
But with the get(), you're declaring a property with a custom getter:
class SomeClass {
val prop
get() = initialisingFunction()
}
That's even more roughly equivalent to:
class SomeClass {
fun getProp() = initialisingFunction()
}
Here, there's no backing field to store the property; every time you get its value, it will call initialisingFunction() and hand you that value directly.
If the property is a var, then you'll also need a custom setter. (Otherwise, the default one would cause a backing field to be created, which it would set, but whose value you'd never see…)
Which version to use will depend on your needs. If the property needs to change in line with something external, then a custom getter is probably needed. It can also be a slightly memory saving if you need to return a value that's the same for all instance of the class. But if calculating the value is expensive, and/or if it's different for each instance, especially if it needs to be mutable, then you'll probably need a property with an initialiser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With