Snail
11/15/2022, 3:29 PMexternal fun getEntity(): Int
external fun getVehicle(): Int
external fun testEntity(e: Int)
external fun testVehicle(e: Int)
All of the integers above are sort of handles that i have no control over. Handles have inheritance, e.g. Vehicle extends Entity.
My goal is to have these handles properly typed to have my own extension functions for them.
I have tried using typealiases. The problem with this approach:
typealias Entity = Int
typealias Vehicle = Entity
fun Entity.test() {} // <--
fun Vehicle.test() {} // <-- Both of these are an error because of conflicting overloads
fun Vehicle.getVelocity(): Float = ...
val entity: Entity = ...
entity.getVelocity() // <-- This works, even though it shouldn't. And is also works for every Int which is completely unacceptable
I also tried using value types. Problem: there is no inheritance. You can sort of trick them into having inheritance, but there's another problem:
interface Handle {
val handle: Int
}
interface IEntity : Handle
interface IVehicle : IEntity
value class Entity(override val handle: Int) : IEntity
value class Vehicle(override val handle: Int) : IVehicle
external fun getEntity(): Entity
external fun getVehicle(): Vehicle
external fun testEntity(e: Entity)
external fun testVehicle(e: Vehicle)
external fun testEntity2(e: IEntity)
external fun testVehicle2(e: IVehicle)
val v = getVehicle()
testEntity(v) // Doesn't work! Vehicle doesn't extend from Entity
testEntity2(v) // Works, but IEntity is not value class so under the hood this boxes value of v, which is not acceptable
I could be missing something, but can't figure out a way to solve this. Thoughts?turansky
11/16/2022, 7:14 AMturansky
11/16/2022, 7:16 AMexternal interface Entity
external interface Vehicle: Entity
Snail
11/16/2022, 12:27 PM