<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="">Arthur,<div class=""><br class=""><blockquote type="cite" class="">There’s a parallel thread going on right now about adding unless/until into the stdlib. I think that’s what you may want.<br class=""></blockquote><br class=""><div class="">`guard` cannot be compared to `if` or `unless` because it's used as an assertion with an early exit, not a condition. Consider the following:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">let x: Int? = 5<br class=""><br class="">guard let x = x where 1...10 ~= x else {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return<br class="">}<br class=""><br class=""></blockquote><blockquote type="cite" class="">// x is unwrapped and between 1 and 10</blockquote><blockquote type="cite" class="">print(x)</blockquote><br class=""></div><div class="">Compare this to `unless`:</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class="">unless let x = x where 1...10 ~= x {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// x is unwrapped and between 1 and 10<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(x)</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return</div><div class="">}</div></blockquote><div class=""><br class=""></div>As you can see, `unless` introduces two new scopes and leads to creation of an indentation, which can result in a pyramid of doom. In addition, `guard` requires you to early exit the scope using `return` or `break`, which is not the case when using `unless`.<div class=""><br class=""></div><div class="">That being said, I don't fully understand the original problem, because this is possible:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">guard<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let col = tableView?.columnWithIdentifier("MyColumn"),<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let headerCell = tableView?.tableColumns[0].headerCell as? MyTableHeaderCell<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>where col != -1<br class="">else {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>NSBeep()</blockquote><font color="#5856d6" class=""><blockquote type="cite" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print("an error occurred")<br class=""></blockquote><blockquote type="cite" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return</blockquote></font><blockquote type="cite" class="">}</blockquote><br class=""><div class=""><div class=""><div class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font color="#929292" class=""><br class="Apple-interchange-newline">Pozdrawiam – Regards,</font></div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><font color="#929292" class="">Adrian Kashivskyy</font></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Wiadomość napisana przez Jeff Kelley via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> w dniu 14.12.2015, o godz. 03:45:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">The first line of your example would need to use <font face="Menlo" class="">self.tableView</font>, which is optional, so <font face="Menlo" class="">col</font> would be optional as well. The assignment happens after the unwrap.<div class=""><br class=""></div><div class="">I find this a lot in my code where I need to chain together a string of optional unwrapping and assignment from those optionals, like so:</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class=""><font face="Menlo" class="">if let foo = foo, bar = foo.bar, baz =foo.baz {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">In that case, if <font face="Menlo" class="">bar</font> is a non-optional property of <font face="Menlo" class="">foo</font>, I need to rewrite it like this:</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class=""><font face="Menlo" class="">if let foo = foo, baz = foo.baz {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let bar = foo.bar</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">These examples aren’t too bad, but as you add more layers of unwrapping, it gets more difficult to do without multiple levels of indentation—though this is probably more a code smell than anything else.<br class=""><div class=""><br class=""></div><div class=""><br class=""><div class="">
<div style="font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; line-height: normal; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; " class=""><div class="">Jeff Kelley</div><div class=""><br class=""></div><div class=""><a href="mailto:SlaunchaMan@gmail.com" class="">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" class="">@SlaunchaMan</a> | <a href="http://jeffkelley.org/" class="">jeffkelley.org</a></div></div></span></div></div></div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2015, at 3:58 PM, Zef Houssney via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Jacob, you actually don’t need to have the redundant guard statement. If you want to assign something to a constant or variable, you just do that before the `guard let`, and you can still use it in the `where`:<br class=""><br class="">let col = tableView.columnWithIdentifier("MyColumn")<br class="">guard let tableView = self.tableView where col != -1 else {<br class=""> NSBeep()<br class=""> print("an error occurred")<br class=""> return<br class="">}<br class=""><br class="">Zef<br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On Dec 12, 2015, at 11:23 AM, Al Skipp via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">How about extending ‘NSTableView’ with a subscript?<br class=""><br class="">extension NSTableView {<br class=""> subscript(columnID columnID: String) -> Int? {<br class=""> get {<br class=""> let c = columnWithIdentifier(columnID)<br class=""> return c >= 0 ? c : .None<br class=""> }<br class=""> }<br class="">}<br class=""><br class="">tableView[columnID: "MyColumn"]<br class=""><br class=""><br class="">It doesn’t address the general case, but it does introduce a more Swifty way of dealing with non-existent return values in this use case and would enable you to use it with guard.<br class=""><br class="">Al<br class=""><br class=""><blockquote type="cite" class="">On 12 Dec 2015, at 17:43, Jakob Egger via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class="">At the moment "guard let" can only be used for unwrapping optionals. It<br class="">would be really nice if it could also be used with non-optional values.<br class="">For example, I'd like to write code like the following<br class=""><br class="">guard <br class=""> let tableView = self.tableView,<br class=""> let col = tableView.columnWithIdentifier("MyColumn") where col != -1<br class="">else {<br class=""> NSBeep()<br class=""> print("an error occurred")<br class=""> return<br class="">}<br class=""><br class="">This is not possible, because the second let assignment is non-optional,<br class="">so I have to write it like this:<br class=""><br class="">guard let tableView = self.tableView else {<br class=""> NSBeep()<br class=""> print("an error occurred")<br class=""> return<br class="">}<br class="">let col = tableView.columnWithIdentifier("MyColumn")<br class="">guard col != -1 else {<br class=""> NSBeep()<br class=""> print("an error occurred")<br class=""> return<br class="">}<br class=""><br class="">This leads to a lot of duplicated error handling code.<br class=""><br class="">Ideally, I'd also like to mix let & where clauses in a single guard<br class="">statement, like this:<br class=""><br class="">guard <br class=""> let tableView = self.tableView,<br class=""> let col = tableView.columnWithIdentifier("MyColumn") where col !=<br class=""> -1,<br class=""> let headerCell = tableView.tableColumns[col].headerCell as?<br class=""> MyTableHeaderCell<br class="">else {<br class=""> NSBeep()<br class=""> print("an error occurred")<br class=""> return<br class="">}<br class=""><br class="">What do you think? Right now I always end up writing a lot of separate<br class="">guard statement, and I have a lot of repeated error handling code.<br class=""><br class=""><br class="">Best regards,<br class="">Jakob<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Q0Y0L54uOhrrHtFzGFlMmzHCkni7p1uaN05dut7Y5S6Re0-2FiKBq0kwsRe9D13x-2FRyMw0cqMCpdWSe3DVyyOA9sygBuN5-2BaUB6rvKULA2n75YZhsL2mlg0OtbYbimL1of7s9cFQbqyUcCqy-2BY6Ct7GqjYPVMGZOaupasOumQPqqUcrNb5thHnn2WewLofQ9gnPXvVP3z9J2PqGhvaEGv9kEsSURktWodPa8GF4lGrRco-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></div></div></body></html>