Kotlin language recently gained some traction owing to Google promoting it as a primary language for writing android apps. Kotlin has already been for a while in production owing to it’s creator JetBrains incorporating it in own tools, such as Intellij IDEA and so forth.
One, amongst many other neat features, is extension functions. In depth documentation can be found on official kotlin website.
About extension functions
In short – extension functions provide us useful tool to extend any existing class, no matter if it’s part of JDK, some 3rd party library or even own, custom ones.
Example, taken from this project, is the following one:
fun Random.betweenInclusive(lower: Int, upper: Int) = this.nextInt(upper - lower) + lower
Here we extend
java.util.Random class from JDK, with
betweenInclusive(lower: Int, upper: Int) extension method. This method helps us generate random integer between two numbers, lower and upper bounds, inclusive. Project contains couple of useful extension functions, implemented in
rs/dodalovic/extension_functions/Students.kt kotlin file. Other examples are extending
List with additional capabilities, taking advantage of functional programming applied on collections. Continue reading