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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.