is there any way to say that all subtypes of a `se...
# language-proposals
e
is there any way to say that all subtypes of a
sealed interface
should be data types?
sealed data interface
could perhaps be a nice addition? I would like to use
copy
with the subtypes.. 🙂 apologies for the low-effort proposal.. just want to get some discussion ✌️
d
Hmm, I think the thing you are realizing here is that a data class is both a language construct but also a hidden interface, i.e. it's like data classes implement the interface
Copyable
, so I wonder if it would make sense or even be possible to generate the byte code for data classes in such a way that they all implement this interface. If that were the case, you could write your sealed interface like so:
sealed interface Foo : Copyable
and both data classes or other classes implementing this interface would work for it
s
That would only work for properties that are also
abstract val
in the
sealed class
though. Since at the level of
Foo
you need to know which properties are common so they can be copied. Optics solves these problem but would love to have something in the language instead of external support. They touch upon it in the https://github.com/Kotlin/KEEP/blob/master/notes/value-classes.md KEEP.
d
Oh yeah I forgot that copy has all of the constructor parameters so you can selectively modify them in your copy function call
myObj.copy(name = "something different")
that makes it a lot more tricky than I thought
So this basically means that each data class'
copy
method has a different signature whereas functions like
equals, toString and hashCode
always have the same signature
👌 1
m
Though there can also be a second abstract
copy()
with no parameters 🤔
k
You could just add a
copy
method to the interface and in each data class add
fun copy() = copy()
... errr, wait, that doesn't work