我只是在玩弄一些函数式编程技术,并试图实现一个列表的泛型drop函数。然而,类型似乎正在变得越来越少。我想知道为什么我不能重用声明的泛型类型。
IDE不希望我重用泛型类型
sealed class List<out A> {
fun <A> drop(n: Int): List<A> {
fun go(n: Int, l: List<A>): List<A> = when (n) {
0 -> l
else -> go(n - 1, l.tail())
}
return go(n, this)
}
}IDE将显示以下内容
Type mismatch.
Required:
List<A#1 (type parameter of chapter3.List.drop)>
Found:
List<A#2 (type parameter of chapter3.List)>对于内部局部函数,这是不可能的吗?
发布于 2021-08-11 03:52:05
您不需要在函数中再次重新定义A类型,它来自密封的类
sealed class List<out A> {
fun drop(n: Int): List<A> {
fun go(n: Int, l: List<A>): List<A> = when (n) {
0 -> l
else -> go(n - 1, l.tail())
}
return go(n, this)
}
}https://stackoverflow.com/questions/68701496
复制相似问题