I have the following
abstract interface Vec2t<T : Number> {
var x: T
var y: T
}
data class Vec2(override var x: Float, override var y: Float) : Vec2t<Float>
and I have an interface where I define several operations, e.g:
interface fun_vector2_common {
fun abs(res: Vec2, a: Vec2): Vec2 {
res.x = glm.abs(a.x)
res.y = glm.abs(a.y)
return res
}
}
Is it possible to implement, let's say abs
, by using generics?
interface fun_vector2_common<T : Number> {
fun abs(res: Vec2t<T>, a: Vec2t<T>): Vec2t<T> {
res.x = glm.abs(a.x) // error
res.y = glm.abs(a.y) // error
return res
}
}
And then the corresponding glm.abs()
based on the type will be called?
The above code fails because it expects, obviously, a glm.abs(n: Number)