Learning Kotlin: The awesome that is the backtick

Submitted by Robert MacLean on Thu, 07/05/2018 - 09:00

More Information

  • This is the 14th post in a multipart series.
    If you want to read more, see our series index

The backtick in Kotlin is meant to allow Kotlin to use keywords or, since Kotlin is meant to interop with Java, allow Kotlin to call Java functions that might conflict with Kotlin; for example, this won't work

  1. val class = "school"
  2. println(class)

If we wrap class in backticks (as it is a keyword) then it works just fine:

  1. val `class` = "school"
  2. println(`class`)

This is nice... however it can be used for function names too, for example with tests we might use underscores to make the name verbose:

  1. fun should_return_true_for_values_of_one() {

however...

  1. fun `should return true for values of one`() {

Yes! That is a function name with REAL SPACES in it! AWESOME!