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.
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