Renaming a generated class name for top-level functions

To demonstrate the renaming of a generated class, we'll make use of the @JvmName annotation within our GreetingFunctions.kt file. Here, we've added the @JvmName annotation to our GreetingFunctions.kt file using the @file use-site target (we'll talk more about use-site targets later on in this chapter):

@file:JvmName("GreetingsHelper")// GreetingFunctions.ktfun sayHello(name: String) = println("Hello $name!")

Within the @JvmName annotation, we've specified the "GreetingsHelper" string as the name we'd like to use for the compiler-generated class. After this addition, calling the sayHello() function from Kotlin is unchanged:

// Main.ktfun main() {    sayHello("Nate")}

But now, when calling ...

Get Mastering Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.