Pete Letkeman wrote:I did recently see an article stating that extension methods get compiled into static methods when transformed into bytecode.
As a result the more extension methods you have, the more static methods you have, and people have different views on static methods.
If you write Java code with lots of static methods, then you're probably doing something wrong - you're most likely writing non-object-oriented, procedural code. But that is not really relevant for Kotlin extension methods - the fact that the Kotlin compiler happens to implement extension methods under the covers using static methods on the JVM doesn't make extension methods somehow bad.
Extension methods are a way to add methods to existing classes after the fact - or, that's what they seem to be at first sight.
In Java, people often create "utils" classes. Maybe you've written a StringUtils class yourself or used methods from such a class. Several well-known libraries have their own sets of "utils" classes, for example Apache Commons Lang and Spring both have their own StringUtils classes with utility methods for strings.
With Kotlin, instead of creating utility methods in a StringUtils class, you can create extension methods for class String instead. You can call these extension methods on String objects, and calling such a method looks exactly the same as calling any other method from class String:
Extension methods are not really dynamically added to a class - they are instead like any other external method, so they can for example not access the private variables of the class they are designed for.