Kotlin Upper Bound:`:Any` 对 Kotlin 的泛型类型推断有何不同?

Kotlin Upper Bound: What difference does `:Any` make to Kotlin's generic type inference?

跟着Kotlin for Android Developers这本书,我们遇到了扩展函数

fun <T:Any> SelectQueryBuilder.parseList(parser: (Map<String,Any?>) -> T):List<T> = parseList(object:MapRowParser<T>{
    override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
})

而且我不确定为什么需要 :Any

如果我写成 fun <T> SelectQueryBuilder.parseList(...),Android Studio 会抱怨

而当您将 :Any 添加回去时,该错误就会消失。

现在,就我而言,T 应该意味着 T:Any,尽管事实显然并非如此。这是为什么?它有什么不同?

Now, as far as I'm concerned, T should imply T:Any

T 表示 T:Any?,其中 Any? 最接近 Java 的 Object。使用 T:Any 您指定了一个不可为 null 的类型。

:Any 为您的泛型类型参数定义了一个 上限 。正如您在 Kotlin 文档的 Generics: Upper Bounds 章节中所读到的,默认上限为 Any?:

The default upper bound (if none specified) is Any?

因此,<T> 等同于 <T: Any?>