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)
It allows you to call a Java method whose name is a Kotlin keyword. It won't work if you leave out the backticks.
The backtick are a "workaround" to allow you to call methods that have a name representing a Kotlin keyword.
See kotlinlang:
It's because
is
is a reserved keyword in Kotlin. Since Kotlin is supposed to be interoperable with Java andis
is a valid method (identifier) name in Java, the backticks are used to escape the method so that it can be used as a method without confusing it as a keyword. Without it it will not work because it would be invalid syntax.This is highlighted in the Kotlin documentation:
is
in list of Kotlin reserved words To use Kotlin reserved word (such asis
orobject
) for function/class name you should wrap it to backtickshttps://kotlinlang.org/docs/reference/java-interop.html