Discussion
How does Kotlin choose between functions that share the same name? This puzzle shows there’s more to the process than looking at the number of inputs. The distinction between a nullable String? and a non-nullable String can also influence the choice, as can other details of the function’s declaration.
Resolving Functions
A function in a Kotlin program is identified by its signature, which contains not just the function’s name but also its inputs and outputs. That means it’s fine for several functions to share the same name and package, provided they differ in the parameters they accept. Reusing a function name is called overloading the function. We’ve talked about overloads before, in Puzzle 1, Something from Nothing?. When you call ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access