Hi, I've got a data class but in one case I want o...
# getting-started
b
Hi, I've got a data class but in one case I want one attribute of it to be evaluated again on each access (getting the current date basically). How do I do this?
m
custom getter basically. However, the whole purpose of it being a data class will be null and void.
Copy code
data class SomeClass(
    val someField: String,
    val someOtherField: Int
) {
    val currentDate: LocalDate
        get() = LocalDate.now()
}
this is what you can do
b
can I decide that in the constructor without making each property a lambda?
SomeClass(currentDate by x { Date() })
basically I'd like to define the getter in the constructor
because in most cases a static value is just fine, it's just that one exception
don't want to create a separate class for that
m
you can put the delegation in your constructor, but never specify how a field is backed in the constructor itself.
Why does it NEED to be in a constructor?
1
b
because I want to configure it by case basis
usecase here is a download object where in one case I need to set the url parameter to be the current date time stamp
p
sealed class DownloadObject
data class NormalCase : DownloadObject()
class SpecialCase : DownloadObject()