Yesterday I reviewed a piece of code in Swift which included this line:
self.self.someProperty
Which surprised me, because the word self is reserved and used as a reference to the current instance.
At first I checked for that phenomenon in other languages, but all gave errors. Which wasn't a surprise - but still, why in swift does it compile and run?
Second I searched in the internet about this and haven't found anything relevant...
Edit I reproduced that and from my checks:
self.someProperty//exactly the same as:
self.self.someProperty//or as:
self.self.self.self.self.someProperty
Swift documentation gives some sort of explanation:
Every instance of a type has an implicit property called self, which is exactly equivalent to the instance itself.
Which is good and partly helpful, but the way I see it it's still not enough
So I'm asking:
- Why does it work?
- Is there any useful logic behind this?
self
property and some of its uses. – Chattyself
implicit "property", none of the examples use it in a uniquely "property" manner -- all the examples are consistent with it being an implicit parameter of the method (like it is in Objective-C), because they all just useself
directly without qualification. So it doesn't really explain why it exists as a property. – Cofer