[swift-evolution] Allowing `guard let self = self else { … }` for weakly captured self in a closure.

Hoon H. drawtree at gmail.com
Wed Jan 27 09:41:21 CST 2016


Apart of the compiler back-tick bug, I like to proceed this proposal from "Socialize the idea” stage to "Develop the proposal” stage. (https://github.com/apple/swift-evolution/blob/master/process.md#how-to-propose-a-change) by writing a formal proposal using template because I think some people are interested in my proposal.

For Jordan’s comment, I like to point that closure capturing list syntax already allows very similar rebinding of “self” which changes only referencing semantics (strong -> weak), so I think this is pretty consistent.

    { [weak self = self] in 
        guard let self = self else { return }
        print(self)
    }

And I am not sure what to do for the prior proposal "Proposal: weakStrong self in completion handler closures” by Jacob. My original intention was about rebinding of “self”, and then, I realised that eliding “self” qualification can make sense with guard statement. So I think my proposal is different thing, but I have strong sense of duty to do something with it because Jordan explicitly mentioned it. Can someone point me what is the best behaviour in this case?

P.S. I think I have sent an empty message to this mailing list. Sorry for that if it happened. This is actually my first long discussion experience with mailing-list stuff. It’s confusing how to use this mailing list stuff...

— Hoon H.





> On 2016/01/06, at 09:02 AM, Hoon H. <drawtree at gmail.com> wrote:
> 
> Currently, weakly captured `self` cannot be bound to `guard let …` with same name, and emits a compiler error.
> 
> 	class Foo {
> 		func test2(f: ()->()) {
> 			// … 
> 		}
> 		func test1() {
> 			test2 { [weak self] in
> 				guard let self = self else { return } // Error.
> 				print(self)
> 			}
> 		}
> 	}
> 
> Do we have any reason to disallow making `self` back to strong reference? It’d be nice if I can do it. Please consider this case.
> 
> 	class Foo {
> 		func getValue1() -> Int {
> 			return 1234
> 		}
> 		func test3(value: Int) {
> 			print(value)
> 		}
> 		func test2(f: ()->()) {
> 			// … 
> 		}
> 		func test1() {
> 			test2 { [weak self] in
> 				self?.test3(self?.getValue1()) // Doesn't work because it's not unwrapped.
> 
> 				self!.test3(self!.getValue1()) // Considered harmful due to `!`.
> 
> 				guard self != nil else { return }
> 				self!.test3(self!.getValue1()) // OK, but still looks and feels harmful.
> 
> 				guard let self1 = self else { return }
> 				self1.test3(self1.getValue1()) // OK, but feels ugly due to unnecessary new name `self1`.
> 
> 				guard let self = self else { return }
> 				self.test3(self.getValue1()) // OK.
> 
> 			}
> 		}
> 	}
> 
> This also can be applied to `if let` or same sort of constructs.
> 
> Even further, we can consider removing required reference to `self` after `guard let …` if appropriate.
> 
> 	guard let self = self else { return } 
> 	test3(getValue1()) // Referencing to `self` would not be required anymore. Seems arguable.
> 
> I think this is almost fine because users have to express their intention explicitly with `guard` statement. If someone erases the `guard` later, compiler will require explicit self again, and that will prevent mistakes. But still, I am not sure this removal would be perfectly fine.
> 
> I am not sure whether this is already supported or planned. But lacked at least in Swift 2.1.1.
> 
> — Hoon H.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 



More information about the swift-evolution mailing list