What are Extension Functions?
Kotlin allows you to "extend" a class with new functionality without having to inherit from the class or use design patterns like Decorator. You can even extend classes that you don't own (like
String, List, or Android's View).Extensions are resolved statically, meaning they don't actually modify the class they extend.
1. Syntax
To define an extension, prefix the function name with the Receiver Type (the name of the class you are extending).
kotlin
2. Using this
Inside an extension function, you can use the keyword
this to refer to the receiver object (the instance the function is called on).kotlin
3. Extension Properties
You can also add properties to classes in a similar way. Note that extension properties cannot have a "backing field" (they must have an explicit getter).
kotlin
4. Why use them?
- Readability: They make code look like it belongs to the object (e.g.,
user.isValid()instead ofUserUtils.isValid(user)). - Clean API: You can add domain-specific logic to general types.
- Null Safety: You can even define extensions on nullable types!
kotlin
Summary Checklist
- Extensions add methods to a class without inheritance.
- They are resolved statically (no class modification).
- Use
ReceiverType.functionNamesyntax. - Use
thisto access the instance. - Perfect for utility methods and fluent APIs.
- Can be defined on nullable types for extra safety.