What are the backticks used for in the snippet below?

Why add them around the fun is(amount:Int ):Boolean { ... }?

verifier.`is`(amount)

推荐答案

这是因为is在Kotlin 是一个保留的关键字.由于Kotlin被认为可以与Java互操作,并且is在Java中是一个有效的方法(标识符)名称,因此使用反号对该方法进行转义,以便可以将其用作方法,而不会将其混淆为关键字.如果没有它,它将无法工作,因为它将是无效语法.

这将突出显示in the Kotlin documentation:

转义Kotline中的关键字Java标识符

Some of the Kotlin keywords are valid identifiers in Java: in, object, is, etc. If a Java library uses a Kotlin keyword for a method, you can still call the method escaping it with the backtick (`) character

foo.`is`(bar)

Kotlin相关问答推荐

Jetpack Compose Material3和Material2 Slider onValueChangeFinded()的行为不同

在 Kotlin 中实现并输入 Either

Kotlin 函数中接收者和参数的类型相同

如何使用 Firebase 和 Kotlin 在文本 (Jetpack Compose) 中显示当前用户名?

Kotlin 编译器在构造函数中报告未使用的表达式,以便构建器采用 vararg lambda

在 Kotlin 中,::class.simpleName是做什么的?

有没有办法重用 Job 实例?

找不到 androidsdk.modules

如何在 android jetpack compose 中相互重叠列表项?

Kotlin 语言是如何用 Kotlin 编写的?

将多个 Kotlin 流合并到一个列表中,而无需等待第一个值

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

Lint 错误:可疑的相等判断:在 Object DiffUtilEquals 中未实现 equals()

如何将 CameraView 与 Jetpack Compose 一起使用?

使用 rxbinding 时我应该取消订阅吗?

将字符串转换为HashMap的最简单方法

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?

如何将 Kotlin 的 `with` 表达式用于可空类型

为什么在 Kotlin 中return可以返回一个return?

如何在 spring-boot Web 客户端中发送请求正文?