Hello! Hello I have a few data classes, all of the...
# announcements
b
Hello! Hello I have a few data classes, all of them have the same field (val time) . And in several places I have code like this = obj.copy(time = newTime). Can I make an extension function that would suit all of such classes?
d
Copy code
interface HasTime {
    val time: Long

    fun copy(time: Long): HasTime

}

data class Time1(override val time: Long) : HasTime
data class Time2(override val time: Long) : HasTime
That works.
If one of the data classes has more members you need to override
copy
by hand, which is not that pretty.
k
Why do you need an extension function for this if the call is only a single line? All the extension function would do is pass the parameter to the copy function.
👍 1