I have an interface ```interface HasFoo { val...
# getting-started
y
I have an interface
Copy code
interface HasFoo { 
    val foo: Foo
}
I want something like
Copy code
sealed interface UpdateableFoo : Foo {
    fun updateFoo(newFoo: Foo): Self
}
where
Self
is a self type (not supported in Kotlin, as per my understanding). what's the least messy way of doing this? should I just not use an interface, and use an extension function
fun <T: HasFoo> updateFoo(newFoo: Foo): T
instead? (edit: actually, I don't think I can do that, since
HasFoo
isn't sealed and so I can't enumerate the `HasFoo`s inside
updateFoo
...?)
e
self-types are sort of possible to encode in Java's type system (which means they're sort of possible in Kotlin)
Copy code
interface Iface<T : Iface<T>> {
    fun copy(): T
}
class Impl : Iface<Impl>
but they're impossible to work safely with in many ways
thank you color 1
y
I see. so for something like this usecase, it might be better to forget about generics/code reuse and add a concrete
copy()
function to the relevant classes
y
You could only enforce it "contractually" by having the extension fun unsafe-cast to
T
. That's the simplest way I'd do it. I don't think it's unreasonable to expect implementers to return an instance of that same class (you could even do
reified
if you want the cast to be checked)
thank you color 1