Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do grave accents do/mean in Kotlin?

Tags:

kotlin

In as many expressions/definitions as possible please.

I'm writing a test function, where after the call fails, the function returns:

`this `fails with` "the state is propagated"`

(with the grave accents surrounding fails with ^ i don't know how to escape, sorry)

like image 422
mleafer Avatar asked Jul 12 '17 19:07

mleafer


People also ask

What is a grave accent used for?

The grave accent marks the height or openness of the vowels e and o, indicating that they are pronounced open: è [ɛ] (as opposed to é [e]); ò [ɔ] (as opposed to ó [o]), in several Romance languages: Catalan uses the accent on three letters (a, e, and o).

Where is grave accent?

The grave accent (`) is under the tilde (~) key on your keyboard, as shown in Figure 8. Pressing the grave key while hovering over any part of the interface blows it up to full-screen size.

What is the difference between a grave and an acute?

The difference between an acute accent and a grave accent is in their sound in the spoken word. An acute accent is spoken with a sharp pitch, while a grave accent is spoken with a loud, heavy tone. Each accent marks the stressed vowel of words in several languages.

How do you type a letter with a grave accent?

Microsoft Windows users can type an "à" by pressing Alt + 1 3 3 or Alt + 0 2 2 4 on the numeric pad of the keyboard. "À" can be typed by pressing Alt + 0 1 9 2 . On a Mac, you hold ⌥ Option + ` , and then let go and type a .


2 Answers

You want to use them when something is a Kotlin keyword (like Java's System.in) but you need to call it. Then you can do

System.`in` 

instead to make it work.

You can also use this in variables, functions, classes and any other identifiers. There is a small paragraph on this topic on Kotlin's documentation.

like image 178
Mibac Avatar answered Nov 04 '22 02:11

Mibac


Actually, it is more than that.

You can use any class, function, variable, or identifier whose name contains spaces or symbols with grave accents.

class `Class name with spaces` {
    fun `method name with spaces, +, -`(`a parameter`: Int) {
        val `variable?!` = `a parameter` + 1
        println(`variable?!`.toString())
    }
}

fun main(args: Array<String>) {
    val instance = `Class name with spaces`()
    instance.`method name with spaces, +, -`(100)
}

This is a compilable and working code: Result

This is often used in testing, in order to make the test method names self-explanatory.

class OperationsUnitTest {
    @Test
    fun `addition should be commutative`() {
        ...
    }
}
like image 40
Naetmul Avatar answered Nov 04 '22 01:11

Naetmul