Dávid
10/21/2020, 10:22 AMfun <data T: MyInterface> T.doSomethig() = copy(...)
And I could get access to beautiful concepts like copy which comes with the data
modifier on a class.
Until this is functionality is implemented, do you know of a good way to do assert that the copy method exists in this scope?kralli
10/21/2020, 10:54 AMinterface A {
val a: String
}
data class B(val b: Int, val c: Int) : A {
override val a: String
get() = (b + c).toString()
}
A
is implemented by B
but the declared property a
of A
is not part of the data class’ constructor and therefor not part of its copy function. Furthermore the copy function contains to properties unknown to the interface A
, making calling the copy function impossible.kralli
10/21/2020, 10:59 AMinterface A {
val a: String
fun copy(a: String = this.a): A
}
data class B(override val a: String) : A
This could have been a way of partially solving your problem, if it wasn’t forbidden by the compiler: Function 'copy' generated for the data class has default values for parameters, and conflicts with member of supertype 'A'
Dávid
10/21/2020, 11:04 AMephemient
10/21/2020, 11:05 AMcopy
thoughDávid
10/21/2020, 11:06 AMdata class
?ephemient
10/21/2020, 11:06 AMdata class B
worked,
data class C(
override val a: String,
val b: String
) : A
wouldn't have the same copy methodephemient
10/21/2020, 11:06 AMfun copy(a: String = /*default*/, b: String = /*default*/)
methodDávid
10/21/2020, 11:06 AMephemient
10/21/2020, 11:08 AMephemient
10/21/2020, 11:11 AMfun <T> T.doSomething(lens: Lens<T, String>): T =
this.set(lens, "world")
B("hello").doSomething(B::a)
ephemient
10/21/2020, 11:11 AMDávid
10/21/2020, 11:16 AMkralli
10/21/2020, 11:17 AMfun copy(a: String,...)
and I don’t think it ever should. This is quite a corner case because it would only work with inlining and there probably is an easier and better solution to your problem.Dávid
10/21/2020, 11:19 AMDávid
10/21/2020, 11:20 AMDávid
10/21/2020, 11:20 AMDávid
10/21/2020, 11:21 AMephemient
10/21/2020, 11:42 AMDávid
10/21/2020, 12:56 PM