Hey Chat! I've hit a wall in this problem that i c...
# javascript
s
Hey Chat! I've hit a wall in this problem that i can't solve elegantly. I have a system with external definitions like this:
Copy code
external 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:
Copy code
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:
Copy code
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?
t
You can use non-opaque external interfaces to solve your problem
Copy code
external interface Entity
external interface Vehicle: Entity
s
Good idea! This really solves it for me, thank you!