Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Back-Tick escaping in method names: How does it work?

In Kotlin, it's possible to name a method using back-ticks like this:

fun `i am a test method`(){
   Assert.assertEquals("x", "x")
}

The compiler generates a method with underscores instead of blanks: "i_am_a_test_method", which seems reasonable as the JVM does not permit methods with blanks afaik. How can Junit and/or Gradle report these tests with the back-ticked name though?

like image 773
s1m0nw1 Avatar asked Aug 18 '17 05:08

s1m0nw1


People also ask

How do you name a function in backticks in Kotlin?

In the Kotlin language, there is a means of specifying the name of a function in backticks – ` `. In this case, the function name can contain spaces or other non-standard characters. In the most general use, the declaration of such a function looks like this: fun `set_of_characters` (list_of_parameters):return_type { // ... }

Why does Kotlin have backticks for Interop in Java?

This presents some issues with Java interop, and in order to get around this Kotlin lets you surround those words with backticks so they don’t cause the compiler to yell at you. For example, if you are in Kotlin and calling a Java method called is, you have to escape it like this:

Can you call Java methods from a Kotlin file?

These might be cases where Java methods are called from code in a Kotlin language file. An example of this use is the use of various Java reserved keywords that cannot be used as names for functions in the Kotlin language (for example, the is keyword); making the use of functions more clear with the help of whole sentences, taken in back quotes.

What are the different scope functions in Kotlin?

Kotlin provides a set of functions to execute a block of code in the context of a given object: let, run, with, apply, and also. For the guidance on choosing the right scope function for your case, refer to Scope Functions. When writing libraries, it's recommended to follow an additional set of rules to ensure API stability:


1 Answers

In a Java method descriptor, several characters have a special meaning, namely [ ( ) / and ;. The space doesn't have any special meaning, and therefore it can be used directly in a method name; that's exactly what the compiler does. The spaces are not converted to underscores.

like image 103
yole Avatar answered Nov 03 '22 01:11

yole