What are the backticks used for in the snippet below?
Why add them around the fun is(amount:Int ):Boolean { ... }
?
verifier.`is`(amount)
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 (`) characterfoo.`is`(bar)