Is "guard self = self" supported? (Answer: Yes in 4.2)


#1

It looks like in Swift 4.2 I can write the code below and it compiles.

There was a proposal (SE-0079) to make this work which was deferred.

Is this now supported in the language to make a weak self into a strong self?

DispatchQueue.main.async { [weak self] in
                    
    guard let self = self else {
        return
    }

   // Do stuff with self, knowing it is held strongly after the guard statement
}

(Jordan Rose) #2

Oops. It wasn't supposed to be supported, but I suppose we're going to have to stay source-compatible. @Slava_Pestov, @rintaro, any idea how this changed?


(Benjamin Mayo) #3

AFAIK this was discussed in this thread, and I believe the behaviour was enabled in this PR. I'm not sure why that proposal (which predates the more recent thread) still says 'Deferred' though.


#4

Thank you! Glad it's supposed to be in 4.2.


(Jordan Rose) #5

My mistake! Thanks, Benjamin.