Kotlin is Joy (caveat: Extension Functions)
Rahul has an excellent summary — https://goo.gl/8HZDSC — of a host of time-saving features in #Kotlin (TL;DR “Why aren’t you using Kotlin yet? Also, #JetBrains?”) including #Anko, Data Classes, and Extension Functions.
The last one caught my eye, it’s a ludicrously useful feature, and also one that can (will!) trip up a lot of people.
Extension Functions in Kotlin are simply plain old static functions that implicitly take an instance of the receiver class as a parameter when the function is defined, and then operate on it. You, therefore, have access to all members of the class inside your function block.
There is no connection to the receiver class in any other form The target classes are not modified. Extensions are visible from Java reflection as static methods in the classes they are defined in (.i.e. in package-classes for top-level extension functions). (°)
The issue?
If you define an extension function with the same signature as a member function inside the receiver class, the member function will be the one that “wins”.
That’s good, right?
Well yes, but you don’t know this happened! I mean, yes, the compiler issues a warning, but hey, you can have a lot of warnings (°°).
Life would be so much better if it threw an error, but it doesn’t.
So, be warned!
(°) Andrey Breslav here — https://goo.gl/f2irP8
(°°) I know, I know, “No Warnings style”. Relying on developers to be perfect is a valid strategy, right?
(°°) I know, I know, “No Warnings style”. Relying on developers to be perfect is a valid strategy, right?
Comments