If I have a jar, on the classpath, where I've created an extension function on say the String class for argument's sake and I have another jar with the same extension function on String, how will Kotlin resolve the two?
I presume if both functions are defined in the same packages then there will be a clash?
But if different packages, how I can distinguish the two extensions?
So since extension function in kotlin is just static function, other functions will be distinguish by import.
Also you can make alias for one of extension functions for more readability:
Indeed, if they're in the same package, it won't compile. For the other scenario, let's say you have two files with two different packages, containing extension functions with the same signature:
First file:
Second file:
And this file where you're trying to use it:
IntelliJ will actually give you an import dialog where you can choose which one you want to use:
You can import one of them like this:
And if you need to use the other one as well, you can rename it with the
as
keyword. This keyword works for imports in general, classes with the same name, etc.So this program compiles and prints
4284
:As a quick note, the one you import with the
as
keyword will be slightly harder to use, as autocomplete doesn't seem to pick it up well, selecting the second option here just completes the call to42.print()
.