Search code examples
androidkotlinkeywordbackticksdata-class

Why does Kotlin data class objects have backticks?


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.


Solution

  • 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.