Why 'self.self' compiles and run in swift?
Asked Answered
R

2

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?
Rhodium answered 28/12, 2015 at 19:58 Comment(3)
I see someone already answered the question, but here is some further info on the self property and some of its uses.Chatty
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.Rhodium
@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.Cofer
G
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"
Gibeon answered 28/12, 2015 at 20:1 Comment(5)
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?Rhodium
Yes. Being overly selfish doesn't have any benefit.Joist
@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.Gibeon
@Gibeon 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 ?Rhodium
@Gibeon From my experience sometimes small things can make big differences and I have a feeling that this might be the case.Rhodium
Q
5

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

Quadrillion answered 28/12, 2015 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.