https://kotlinlang.org logo
Title
g

Guru

03/20/2019, 4:42 AM
How to restrict any one to add an extension method/properties to a particular class? My team works at different location and they are trying to put similar functionality on the same class by creating extensions in two different *.kt files. They were not known that they already have similar functionality being implemented in other module unless we integrate finally to a single file/module... there is a code reusability issue here.. any suggestions ?
k

karelpeeters

03/20/2019, 8:35 AM
Put the functionality people expect in the class itself 😒imple_smile:, otherwise I don't think it's possible to limit extension functions.
j

Jiddles

03/20/2019, 11:15 AM
I think this typically a problem with a extension functions, it’s really easy to add new ones and not know whether something similar has been implemented. Having all extensions/similar extension in the same files can help, for example: - extensions/RxExtensions.kt - extensions/AndroidExtensions.kt etc. Also having a good PR process/Danger rules/custom linting rules to limit or watch for new ones being added should help resolve the problem to some extent
👍 1
a

Alowaniak

03/20/2019, 12:12 PM
I don't think this is an 'extension functions' problem though, it could happen with static helper functions or w/e in java too It's just a code duplication problem, I think it's important to spot things like this in reviews etc
👍 1
1
👆 2
g

Guru

03/22/2019, 8:36 AM
I understand if this is a static function. But, with extension function its different. you are sharing your class data/properties with the function itself. it would have been nice if we could use a keyword like 'restrict' to avoid a class to have extension function. until then what you said is true. Spot things in reviews 🙂
k

karelpeeters

03/22/2019, 2:45 PM
There's no sharing going on, extension function are pretty much just syntax sugar for static functions.