8

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:

  1. Why does it work?
  2. Is there any useful logic behind this?
Community
  • 1
  • 1
Nikita Kurtin
  • 5,889
  • 4
  • 44
  • 48
  • 1
    I see someone already answered the question, but [here](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html#//apple_ref/doc/uid/TP40014097-CH15-ID238) is some further info on the `self` property and some of its uses. – Joachim Isaksson Dec 28 '15 at 20:07
  • 1
    I've read it, thank you. I also added this link to the question -> and it gives some sort of explanation. But it's not really answering _Why_ it's more like stating a fact, and not gives any use for _self.self_ I mean there is a reason why the same thing gives an error in other languages So I guess there should also be a reason why it works here and (more important) where should it be used. – Nikita Kurtin Dec 28 '15 at 20:38
  • 1
    @JoachimIsaksson: Although that section claims to talk about a `self` 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 use `self` directly without qualification. So it doesn't really explain why it exists as a property. – newacct Dec 28 '15 at 23:28

2 Answers2

7

It's because the .self property of an object is that object itself. So your second self changes nothing.

You could, in fact, extend this game indefinitely (or as long as your patience holds out):

let s = "Hello".self.self.self.self.self.self.self.self
// s is still simply "Hello"
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I also found it just now - but what is the usage of it? Or it's just because it's implicit property added to any instance? – Nikita Kurtin Dec 28 '15 at 20:05
  • 10
    Yes. Being overly selfish doesn't have any benefit. – timbo Dec 28 '15 at 20:12
  • 1
    @NikitaKurtin It is often useful when the object in question is a _class_ object; there are situations where adding `.self` to the name of a class, or to some property or variable that holds a class, is helpful to the compiler. Otherwise, it isn't something you would actually do. – matt Dec 28 '15 at 20:41
  • 1
    @matt Can you give an example of: _situations where adding .self to the name of a class, or to some property or variable that holds a class, is helpful to the compiler_ ? – Nikita Kurtin Dec 28 '15 at 21:32
  • @matt From my experience sometimes small things can make big differences and I have a feeling that this might be the case. – Nikita Kurtin Dec 28 '15 at 22:22
5

Yup, no matter how many .self's you have after a string, it's still just itself

fi12
  • 495
  • 11
  • 30