Junior — Middle
Why is it not recommended to add additional properties in extensions in programming languages?
sobes.tech AI
Answer from AI
In Swift, it is not recommended to add stored properties in extensions because extensions can only contain computed properties and methods. This is because extensions cannot modify the internal structure of a type — they cannot add additional memory to store new properties.
Attempting to add a stored property in an extension will result in a compiler error. Instead, you can use computed properties or associated objects (via Objective-C runtime) to store additional data, but this complicates the code and reduces its clarity.