Kotlin 如何细化返回类型的泛型参数?

Kotlin 如何细化返回类型的泛型参数?,kotlin,generics,Kotlin,Generics,我需要的一个例子: interface Interface1 sealed interface Interface2<P0: Interface1> interface Interface3<P1: Interface1, P2: Interface2<out P1>> { // get error: Type parameter cannot have any other bounds if it's bounded by another typ

我需要的一个例子:

interface Interface1

sealed interface Interface2<P0: Interface1>

interface Interface3<P1: Interface1, P2: Interface2<out P1>> {
    // get error: Type parameter cannot have any other bounds if it's bounded by another type parameter
    fun <K: P1, G> getInterface2(): G where G: P2, G: Interface2<K> 
}
接口1
密封接口2
接口3{
//获取错误:如果类型参数由另一个类型参数限定,则该类型参数不能有任何其他界限
fun getInterface2():G其中G:P2,G:Interface2
}
所以在Interface3中,我需要返回P2和类P1的具体后代的函数

我需要函数getInterface2()来获取Interface2的子代,其中K作为P0参数。

fun getInterface2():G其中G:P2,G:Interface2
fun <G> getInterface2(): G where G: P2, G: Interface2<out P1>
应该具有相同的效果,因为任何
接口
都扩展了
Interface2

旁注:我认为这是一个过于简单的问题,但是这个签名承诺返回一个
G
,而没有任何方法知道
G
是什么,所以它不能被合法地实现