Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Kotlin method have enclosing backticks?

What are the backticks used for in the snippet below?

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

verifier.`is`(amount) 
like image 712
LF00 Avatar asked May 24 '17 05:05

LF00


2 Answers

It's because is is a reserved keyword in Kotlin. Since Kotlin is supposed to be interoperable with Java and is 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:

Escaping for Java identifiers that are keywords in Kotlin

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) 
like image 58
Andrew Li Avatar answered Sep 27 '22 17:09

Andrew Li


It allows you to call a Java method whose name is a Kotlin keyword. It won't work if you leave out the backticks.

like image 20
wero Avatar answered Sep 27 '22 16:09

wero