thanksforallthefish
10/29/2020, 9:17 AMcopy
and equals
and being able to have an interface would help me doing the refactor incrementally. eg:
interface Test {
val field: String
fun copy(field: String)
}
data class TestData(override val field: String) : Test
but ofc this does not compile. I don’t think it is really doable, but hope is the last to die they say 🙂kralli
10/29/2020, 9:49 AMcopy
function is defined by the constructor of the data class and therefor cannot be constrained by an interface. A data class may choose to implement the interface’s field outside of its constructor; the field would never appear in the copy
function.kralli
10/29/2020, 9:51 AMinterface Test {
val field: String
fun withField(field: String): Test
}
data class TestData(override val field: String) : Test {
override fun withField(field: String): Test = copy(field = field)
}
Vampire
10/29/2020, 9:55 AMcopy
method has default values for the parameters and this is not allowed for an overwriting method also outside data classes.
So in the same sense as
interface IFoo {
fun foo(x: Int, y: Int)
}
class CFoo : IFoo {
override fun foo(x: Int = 0, y: Int = 1) {
}
}
is not allowed, it is also not allowed for data classes copy methodkralli
10/29/2020, 10:22 AMthanksforallthefish
10/29/2020, 10:27 AMstreetsofboston
10/29/2020, 12:33 PMdata _*interface*_
as well or something similar, that would declare the destructuring operator functions and the copy functions....Nir
10/29/2020, 12:35 PMVampire
10/29/2020, 1:39 PMinterface Test {
val field: String
fun makeCopy(field: String = this.field): Test
}
data class TestData(override var field: String) : Test {
override fun makeCopy(field: String) = copy(field)
}
Vampire
10/29/2020, 1:41 PMVampire
10/29/2020, 1:41 PMVampire
10/29/2020, 1:43 PMprintln((test as TestData).copy("jklö"))
streetsofboston
10/29/2020, 1:46 PMdata interface Named {
val firstName: String
val lastName: String
}
...
data class Person(
override var firstName: String,
override var lastName: String,
var age: Int
): Named
...
val named: Named = Person("Johny", "Matheson", 34)
val copyOfRecord = named.copy(lastName = "R. Matheson")
val (_, _, age) = copyOfRecord
Nir
10/29/2020, 1:52 PMNir
10/29/2020, 1:53 PM