3
是否有可能实现一个通用的,比方说,除了类似如下:科特林,通用除了
public abstract interface NumberEx {
abstract fun plus(other: NumberEx): NumberEx
}
abstract interface Vec2t<T : NumberEx> {
open var x: T
open var y: T
fun add(res: Vec2t<T>, a: Vec2t<T>, bX: T, bY: T): Vec2t<T> {
res.x = a.x + bX
res.y = a.y + bY
return res
}
}
因为这里的编译器抱怨a.x + bX
和a.y + bY
:
类型不匹配。要求:t上找不到:NumberEx
我看到“Kotlin,通用成瘾”:< – mklimek
没关系,我们都是kotlin在这里上瘾。在b4 Kotlin匿名 – elect