Bernhard
07/23/2024, 8:56 AMopen class Model {
fun update(): Model {
// logic in here
TODO()
}
}
open class User: Model()
open class SpecialUser: User()
obviously User.update() should return a User and SpecialUser.update() a special user but I don't really see a way of having the type defs work other than overriding it with a more special one in each subtype
PS: it's a JS API, just trying to figure out what the limits of Kotlin's type system are 🙂Riccardo Lippolis
07/23/2024, 9:00 AMopen class Model<out T: Model<T>> {
fun update(): T {
// logic in here
TODO()
}
}
open class User: Model<User>()
open class SpecialUser: Model<SpecialUser>()
Bernhard
07/23/2024, 9:01 AMRiccardo Lippolis
07/23/2024, 9:01 AMBernhard
07/23/2024, 9:02 AMBernhard
07/23/2024, 9:03 AMRob Elliot
07/23/2024, 9:49 AMYoussef Shoaib [MOD]
07/23/2024, 2:23 PMopen class Model {
fun doUpdate() {
// logic in here
}
}
fun <M: Model> M.update(): M = this.also { doUpdate() }
Bernhard
07/23/2024, 2:26 PMBernhard
07/23/2024, 2:26 PMBernhard
07/23/2024, 2:42 PMBernhard
07/23/2024, 2:48 PMfun <T : Model> T.typesafeUpdate(): T = update() as T
Bernhard
07/23/2024, 2:49 PMYoussef Shoaib [MOD]
07/23/2024, 2:50 PMupdate
always returns the same type then this is fine.
I wonder how this is implemented on the JS side? Prototype shenanigans?Bernhard
07/23/2024, 2:56 PMawait this.constructor.update()
Bernhard
07/23/2024, 2:57 PMBernhard
07/23/2024, 2:58 PMYoussef Shoaib [MOD]
07/23/2024, 2:58 PMBernhard
07/23/2024, 2:59 PMBernhard
07/23/2024, 2:59 PMBernhard
07/23/2024, 2:59 PM