6

This is my data class created using a Kotlin data class creator Plugin.

data class ResponseHealthInisghts(
val `data`: List<Data>,
val message: String,
val statusCode: Int
)

This code gets work even if I remove the backticks, I wonder if it's for Java interoperability. But this variable is not a keyword but also it has backticks. why? Based on Why does this Kotlin method have enclosing backticks? this question is is a keyword for both Java and Kotlin but data is not.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77

3 Answers3

4

You can use backticks simply to enclose class, method or variable name

For example it's useful if there are spaces:

class `Final Frontier` {
    fun `out of space`() {
        val `first second`: String?
    }
}

Or as you mention if using Kotlin keyword

If a Java library uses a Kotlin keyword for a method

foo.`is`(bar)

data is a Modifier Keyword

data instructs the compiler to generate canonical members for a class The following tokens act as keywords in modifier lists of declarations and can be used as identifiers in other contexts

And not a Hard Keyword that can't be used as identifier

The following tokens are always interpreted as keywords and cannot be used as identifier

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
2

It allows you to use reserved keywords and operators as names of your variables. The list of those words: https://kotlinlang.org/docs/reference/keyword-reference.html

Eugene Babich
  • 1,271
  • 15
  • 25
  • Could you please elaborate on that, i couldn't understand the above answer – Manoj Perumarath Nov 20 '19 at 12:59
  • 2
    `data` is a modifier keyword. As you have used `data class` convey a special meaning to the compiler. While converting your Java code to Kotlin the converter did not change the variable name in your source rather enclosed it in backticks for proper compilation – Arka Prava Basu Nov 20 '19 at 13:03
  • 1
    Java does not have a keyword `data` hence it is fine to keep `data` as a variable name in Java. But in Kotlin you cant achieve the same without backticks – Arka Prava Basu Nov 20 '19 at 13:05
  • @ArkaPravaBasu but what about 'is', it's a reserved keyword in Java, then how? – Manoj Perumarath Nov 20 '19 at 13:05
  • 1
    data is a *Modifier Keyword* and not a reserved word: https://kotlinlang.org/docs/reference/keyword-reference.html#modifier-keywords – forpas Nov 20 '19 at 13:06
  • 1
    You can use `data` without backticks here, because it's a soft keyword. This example runs fine: https://pl.kotl.in/t78k70IHx – marstran Nov 20 '19 at 13:07
0

Based on this question's answerWhy does this Kotlin method have enclosing backticks? and the comments from @forpas and @marstran I was able to understand my problem.

The is keyword is a hard keyword

Hard Keywords are always interpreted as keywords and cannot be used as identifiers:

so fore interoperability we need to use backticks because Java and Kotlin both have is keyword.

Where data keyword is only available in Kotlin and also belong to the category

Soft Keywords act as keywords in the context when they are applicable and can be used as identifiers in other contexts.

So we can use it with or without backticks.

Also as an additional note you can use bacticks to customize your identifier

var `data is simple` : List<String>

If it shows lint error use

"File | Settings | Editor | Inspections | Illegal Android Identifier" and disable this inspection.

Community
  • 1
  • 1
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77