David Kubecka
05/29/2024, 3:24 PMcopy
in functions which handle polymorphic data classes, i.e. the functions accept an interface as a parameter? Obviously, since the parameter is an interface there's no copy
method available. But my intention is for the concrete implementations of that interface to always be data classes. Is there any way how to use copy
in this situation?David Kubecka
05/29/2024, 3:24 PMDavid Kubecka
05/29/2024, 3:33 PMdata interface
.CLOVIS
05/29/2024, 3:37 PMdata interface
can never be a thing: by nature, something that is purely data cannot be overriden, and by nature, two interface implementations must be different, so they are different data.CLOVIS
05/29/2024, 3:38 PMCLOVIS
05/29/2024, 3:39 PMMichael Krussel
05/29/2024, 3:39 PMCLOVIS
05/29/2024, 3:39 PMDavid Kubecka
05/29/2024, 3:43 PMIn general, an interface is contract of behavior and a data class is a contract of state, they are different conceptsWell, and couldn't be the "data-ness" also considered as kind of behaviour? Because
copy
actually IS behaviour and the only thing I want from the hypothetical interface is to tell the compiler that the implementations are data classes with the copy method.
I guess this will probably quickly hit some limitations in the data class design/technical implementation.David Kubecka
05/29/2024, 3:44 PMWhat I've done is just create a copy function in the interface and have the subclasses implement it by delegating to their copy function.That will work but must be quite verbose, though, no?
CLOVIS
05/29/2024, 3:45 PMcopy
, the compiler has to know all fields. So, if it generated copy
at the interface level, you would be forbidden to add new fields when implementing it. Since all implementations would necessarily be data class
, and they cannot add any new fields, then all implementations are necessarily exactly the same, and thus the interface is useless since it already has a single useful implementation.Michael Krussel
05/29/2024, 3:45 PMMichael Krussel
05/29/2024, 3:47 PMCLOVIS
05/29/2024, 3:52 PM