I've been trying to find the best way to implement a stored property in an extension, and came across this question: Swift extension stored properties alternative. However, I have not found a reason why in the discussion or anywhere else. Is there a reason why stored properties are not allowed in Swift? And if so, what is the reason?
Asked
Active
Viewed 1,446 times
1 Answers
6
Extensions are for extending the functionality of an existing class without changing the memory structure. It's more or less syntactic sugar. Imagine you could add stored properties and methods, what would it be? Nothing else but inheritance. So if you like to add new properties and methods just inherit from the class.
Darko
- 9,213
- 9
- 30
- 43
-
Inheritance is frowned upon in Swift use, no? – Joseph Beuys' Mum Mar 08 '19 at 21:11
-
It’s not at all. It’s just that inheritance is often overused in many languages. The complete Swift standard library doesn‘t need a single inheritance model, it is fully protocol oriented and value based. (only structs, no classes) But sometimes inheritance fits better for the given use case, it depends on many things. – Darko Mar 08 '19 at 21:18