<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="" applecontenteditable="true"><div class="">Hi, Shawn. "Non-escaping" doesn't mean "guaranteed to be called at least once", nor "guaranteed to be called at most once". You'd need both of those conditions to make this okay.</div><div class=""><br class=""></div><div class="">There's been some discussion on the swift-evolution list about adding such an annotation, but nothing's been seriously proposed yet. As an additive change to Swift, it isn't something that needs to happen <i class="">now,</i> just an improvement we could make.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 2, 2017, at 12:33, Shawn Erickson via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I attempted the following code which involves passing a closure – one that doesn't escape obviously – but the compiler complains that I need to make formHeaders and formStream vars.<div class=""><br class=""><div class=""><div class=""> public func finish() throws -> ([String:String],InputStream) {</div><div class=""> let formHeaders: [String:String]</div><div class=""> let formStream: InputStream</div><div class=""> try finish({ (fh, fs, _) in</div><div class=""> formHeaders = fh</div><div class=""> formStream = fs</div><div class=""> })</div><div class=""> return (formHeaders, formStream)</div><div class=""> }</div></div></div><div class=""><br class=""></div><div class="">If I have code like this however the use of let is ok.</div><div class=""><br class=""></div><div class=""><div class=""> public func finish() throws -> ([String:String],InputStream) {</div><div class=""> let formHeaders: [String:String]</div><div class=""> let formStream: InputStream</div><div class=""> if self.finalized.get() {</div><div class=""> formHeaders = ["foo":"bar"]</div><div class=""> //formStream = ...</div><div class=""> }</div><div class=""> else {</div><div class=""> formHeaders = [:]</div><div class=""> //formStream = ...</div><div class=""> // or throw something here</div><div class=""> }</div><div class=""> return (formHeaders, formStream)</div><div class=""> }</div></div><div class=""><br class=""></div><div class="">If the closure is non-escaping and the code as structured the compiler should be able to reason about the initialization correctness of those lets...? ...or am I missing something?</div><div class=""><br class=""></div><div class="">-Shawn</div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>