kotlin - 如何细化返回类型的泛型参数?
问题描述
我需要的一个例子:
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>
}
因此,在 Interface3 中,我需要返回 P2 和类 P1 的具体后代的函数。
我需要函数 getInterface2() 来获取具有 K 作为 P0 参数的 Interface2 的后代。
解决方案
fun <G> getInterface2(): G where G: P2, G: Interface2<out P1>
应该具有相同的效果,因为 any Interface<K>
extends Interface2<out P1>
。
旁注:我认为这是一个过度简化,但这个签名承诺返回 aG
没有任何方法知道是什么G
,所以它不能被合法地实现。
推荐阅读
- java - 如何在 Maven Build 上运行 CamelTest 单元?
- c - 在 ARM 64 位平台上编译 x264 时出错
- c++ - GCC 7.3.0 链接线程库使用 -static 是否存在错误?
- python - 有没有办法仅在维度的子集上使用 numpy.outer ?
- android - Glide 使用 android recyclerview 得到 Forbidden 错误 (403)
- git - 从 mybranch 切换时 git checkout master 返回错误
- reactjs - Matter.js Runner 没有重置(停止)
- git - git fetch 远程分支,但在本地存储库或工作副本中看不到
- microsoft-teams - 在 MS Teams 中部分呈现的自适应卡片图像
- python - 如何删除以python中的某个字符串结尾的数据框中的某些列表成员