Kotlin 中带有扩展的泛型

Generics in Kotlin with extends

我正在尝试将 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 spec

The following pair of declarations is invalid, because there are edges T → S and S → T, forming a cycle:

interface B<T : C<*>>

interface C<S : B<*>>

原因如下:

In its fully expanded form this bound would be infinite. The purpose of this rule is to avoid such infinite types, and type checking difficulties associated with them.

你的情况是V -> P 和P -> V 形成一个循环。