https://kotlinlang.org logo
#announcements
Title
# announcements
b

borboss

08/02/2017, 11:17 AM
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

diesieben07

08/02/2017, 11:23 AM
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

kirillrakhman

08/02/2017, 11:55 AM
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