Kotlin的泛型与扩展
我试图将Java项目移植到kotlin,并有一些问题。 我在Java中使用泛型有一些MVP结构
interface View<P extends Presenter> {} interface Presenter<V extends View> {} interface BaseView<P extends Presenter> extends View<P> {} class BaseActivity<P extends Presenter> extends AppCompatActivity implements BaseView<P> {}
最初两个类,我从IDE错误
interface Presenter<V : View<*>> {} interface View<P : Presenter<*>> {}
我的错误是
*this type parameter violates the finite bound restriction*
Java代码没有任何问题
我想这是Kotlin不允许的。
从Kotlin规范 :
下面的一对声明是无效的,因为存在边T→S和S→T,形成一个循环:
interface B<T : C<*>>
interface C<S : B<*>>
原因是:
在完全展开的形式下,这个界限将是无限的。 这个规则的目的是为了避免这种无限的类型,以及类型检查相关的困难。
在你的情况下,它是V – > P和P – > V形成一个循环。