I am not sure I fully understand the difference between SomeGeneric<*> and SomeGeneric<Any>. I think * represents anything (wild card) and Any represents the object which ALL objects inherit from. So it seems they should be the same, but are they?

推荐答案

It may be helpful to think of the star projection as a way to represent not just any type, but some fixed type which you don't know what is exactly.

例如,类型MutableList<*>表示something的列表(您不知props 体是什么).因此,如果你试图在这个列表中添加一些东西,你不会成功的.它可以是String的列表,也可以是Int的列表,或者是其他东西的列表.编译器根本不允许将任何对象放入此列表中,因为它无法验证列表是否接受此类型的对象.但是,如果您try 从这样的列表中获取元素,您肯定会得到一个Any?类型的对象,因为Kotlin中的所有对象都继承自Any.

From asco comment below:

此外,List<*>可以包含任何类型的对象,但仅限于此

Kotlin相关问答推荐

如何在Jetpack Compose中的列中渲染图像

如何在Kotlin中模拟www.example.com()?

将文本与文本字段的内容对齐

仅某些用户出现 DateTimeFormatter 错误

为什么会出现Kotlin.Unit错误以及如何修复它?

如果带注释的成员未被特定块包围,则发出 IDE 警告

如何在 kotlin 中使用带有泛型的密封类

如何为 Kotlin 中的每个循环设置以避免越界异常

如何通过 compose 处理剪切区域?

内联函数导致单元测试代码覆盖率报告出错

interface扩展

Kotlin 方法重载

`this@classname` 在 Kotlin 中是什么意思?

ActivityOptions.makeSceneTransitionAnimation 在具有多个视图的 kotlin 中不起作用

如何解决:将Java类转换为Kotlin后出现error: cannot find symbol class ...?

编译错误:-Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond

Android Kotlin 创建类实现 Parcelable 在 writeToParcel 方法的 override中给出错误

Kotlin中的Memoization功能

Kotlin Flow 收集后无法执行代码

在 intelliJ 元素中集成 Kotlinx 协程