Extensions in Kotlin and Why They Are Useful

Antfroze / July 09, 2022
3 min read • ––– views

Extensions are a new thing for Java developers, they have been present in C# for a long time but they landed in JVM land for the first time courtesy of Kotlin.
Table of Contents
- What Are They and Why Are They Useful?
- When To Use an Extension Function
- How Do I Create an Extension Function?
- Usage of an Extension Property
- Conclusion
What Are They and Why Are They Useful?
To start, there are two types of extensions in Kotlin, there are Extension Functions and Extension Properties. These are useful because you can extend the functionality of an API without using inheritance or any forms of the Decorator pattern - after defining an extension we can essentially use it as if it was a part of the original API. Having these promotes easy, flexible, readable code.
When To Use an Extension Function
Let's assume that you are in a situation where you need to get the cube of a Double variable num
, you would normally write something like this:
import java.lang.Math
fun main() {
val num = 5.0
print(num * num * num) // Output: 125.0
}
Imagine that you need to do this same calculation in a lot of places in the application. That's a lot of repeating right? It would be super helpful if the privative type Double
had a method cube
that achieves the same result, but is more elegant.
fun main() {
val num = 5.0
print(num.cube()) // Output: 125.0
}
The primitive Double
type doesn't have an cube
function, but you can easily implement this yourself using an extension function!

How Do I Create an Extension Function?
Create the function / file
I usually create a folder named Extensions
at the root of my project to keep things organized. You can either make a new file, or you can write the function in the class where you are wanting to use it.
Here is what the cube
function would look like.
Note: Make sure your function is top level.
fun Double.cube(): Double {
return this * this * this
}
Use the function
Now that we have created the extension, anywhere in our code we can use the cube function on a double variable.
val num = 5.0
print(num.cube()) // Output: 125.0
🥳 Congrats you have made your first extension in Kotlin! Now it's time to see what Extension Properties are.
Usage of an Extension Property
Extension Properties are similar to extension functions, except you can't provide parameters as input to them. This is more fit for our function above because we don't need to use any arguments.
Here's what that would look like.
Note: Make sure your property is top level.
val Double.cubed: Double
get() = this * this * this
Now in our code, we can use it as if we are accessing a class property.
val num = 5.0
print(num.cubed) // Output: 125.0
Conclusion
Overall, extensions are a useful tool to be used thoughtfully, remember the tips below when you use them to make your code more intuitive and readable.
Remember:
- Extensions are resolved statically.
- Member functions will always win.
Happy coding!