Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as DecoratorThis is the very first line of Kotlin extension function documentation. As it states Kotlin extensions are a way to extend a class with new functionality. I agree that there is no actual extension happening and it is resolved statically. But those are implementation details in my opinion. If you look at the examples the doc has, first one is adding a swap functionality to a MutableList class. Swap should have been a responsibility of MutableList class but it is missing. Extension enable us to add such missing functionalities to classes. I really like to think extensions and utility methods are two different entities.
Extension enable us to add such missing functionalities to classes.
Really I don't see such restriction anywhere! I understand that we are free to add new functionality to any class even if the function is not an actual responsibility of the receiver type. Am I missing something there?
There is no such limitation documented anywhere, you can add new functionality to any class even if it is not related, is because it is not possible to figure out whether functionality belong to the class or not. Consider how you use the extension function to understand what I was trying to say. Say you added a method show toast to String class.
When you call it from any class it will be something like this
valname=user.namename.showToast(context)
It has become like name.length() or name.isEmpty(). But the added methods should not be really part of String class. Look at the extensions provided by android-ktx library. It can be found that those extensions belong to specific classes and and are not very generic utility methods
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator
This is the very first line of Kotlin extension function documentation. As it states Kotlin extensions are a way to extend a class with new functionality. I agree that there is no actual extension happening and it is resolved statically. But those are implementation details in my opinion. If you look at the examples the doc has, first one is adding a swap functionality to aMutableList
class. Swap should have been a responsibility ofMutableList
class but it is missing. Extension enable us to add such missing functionalities to classes. I really like to think extensions and utility methods are two different entities.Really I don't see such restriction anywhere! I understand that we are free to add new functionality to any class even if the function is not an actual responsibility of the receiver type. Am I missing something there?
There is no such limitation documented anywhere, you can add new functionality to any class even if it is not related, is because it is not possible to figure out whether functionality belong to the class or not. Consider how you use the extension function to understand what I was trying to say. Say you added a method show toast to String class.
When you call it from any class it will be something like this
It has become like
name.length()
orname.isEmpty()
. But the added methods should not be really part of String class. Look at the extensions provided by android-ktx library. It can be found that those extensions belong to specific classes and and are not very generic utility methods