In Python you can use _ as a variable name. If I write e.g. val _ = 3 in Kotlin IntelliJ gives me an error with:

Names _, __, ___, ..., are reserved in Kotlin

预订的是什么?它们的功能是什么?

推荐答案

The single underscore is already used in several ways where you want to skip a parameter or a component and don't want to give it a name:

These syntax forms were introduced in Kotlin 1.1, and that's why the underscore names had been reserved before Kotlin 1.1. The multiple-underscore names like __, ___ had also been reserved so that they are not misused where previously one would have used a single-underscore name.


As @Willi Mentzel noted in a comment, another use of underscores, though not in a position of an identifier, is separating digit groups in numeric literals:

val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L 

Kotlin相关问答推荐

带有Spring Boot和Kotline的可嵌入实体

使用另一个对象的列表创建对象

如何从 Period.between() 返回的字符串中提取信息? (Kotlin )

Kotlin:我可以将函数分配给 main 的伴随对象中的变量吗?

在 Kotlin 中,如何绑定扩展方法以在接收器范围函数中工作

Mixin 在 Jackson 中添加 defaultImpl 不起作用

为什么没有remember 的 mutableStateOf 有时会起作用?

Kotlin 使用迭代索引过滤 lambda 数组

如何使用 Hilt 注入应用程序:ViewModel 中的上下文?

致命错误 LifecycleOwners 必须在 registerForActivityResult 开始之前调用 register

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

Kotlin 是如何编译的?

Mockito 的 argThat 在 Kotlin 中返回 null

Android Studio 和 Kotlin:Unresolved reference: also

如何使用kotlin中的反射查找包中的所有类

如何捕获传递给模拟函数的参数并返回它?

API 26 上未显示 Android 通知

如何在协程之外获取 Flow 的值?

Kotlin/JS,Gradle 插件:无法加载@webpack-cli/serve命令

Spring Boot:更改属性占位符符号