<div dir="ltr">Hi Adrian,<div><br></div><div>Yes, as noted by others in a separate response, I misunderstood the question. Like you, I still don't see a problem, as the OP is trying to achieve something that can already be done (see one of my previous posts in this thread).</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 16, 2015 at 4:07 AM Adrian Kashivskyy via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Arthur,<div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite">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></blockquote><br></div></div><div style="word-wrap:break-word"><div><div>`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><br></div><div><blockquote type="cite">let x: Int? = 5<br><br>guard let x = x where 1...10 ~= x else {<br><span style="white-space:pre-wrap">        </span>return<br>}<br><br></blockquote><blockquote type="cite">// x is unwrapped and between 1 and 10</blockquote><blockquote type="cite">print(x)</blockquote><br></div><div>Compare this to `unless`:</div><div><br></div><blockquote type="cite"><div>unless let x = x where 1...10 ~= x {</div><div><span style="white-space:pre-wrap">        </span>// x is unwrapped and between 1 and 10<br><span style="white-space:pre-wrap">        </span>print(x)</div><div>} else {</div><div><span style="white-space:pre-wrap">        </span>return</div><div>}</div></blockquote><div><br></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><br></div><div>That being said, I don't fully understand the original problem, because this is possible:</div><div><br></div><div><blockquote type="cite">guard<br><span style="white-space:pre-wrap">        </span>let col = tableView?.columnWithIdentifier("MyColumn"),<br><span style="white-space:pre-wrap">        </span>let headerCell = tableView?.tableColumns[0].headerCell as? MyTableHeaderCell</blockquote></div></div></div><div style="word-wrap:break-word"><div><div><blockquote type="cite"><br><span style="white-space:pre-wrap">        </span>where col != -1<br>else {<br><span style="white-space:pre-wrap">        </span>NSBeep()</blockquote></div></div></div><div style="word-wrap:break-word"><div><div><font color="#5856d6"><blockquote type="cite"><span style="white-space:pre-wrap">        </span>print("an error occurred")<br></blockquote><blockquote type="cite"><span style="white-space:pre-wrap">        </span>return</blockquote></font><blockquote type="cite">}</blockquote><br></div></div></div><div style="word-wrap:break-word"><div><div><div><div><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;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><font color="#929292"><br>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;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><font color="#929292">Adrian Kashivskyy</font></div>
</div>
<br><div><blockquote type="cite"><div>Wiadomość napisana przez Jeff Kelley via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> w dniu 14.12.2015, o godz. 03:45:</div></blockquote></div></div></div></div></div></div><div style="word-wrap:break-word"><div><div><div><div><div><blockquote type="cite"><br><div><div style="word-wrap:break-word">The first line of your example would need to use <font face="Menlo">self.tableView</font>, which is optional, so <font face="Menlo">col</font> would be optional as well. The assignment happens after the unwrap.<div><br></div><div>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><br></div><div></div><blockquote type="cite"><div><font face="Menlo">if let foo = foo, bar = foo.bar, baz =foo.baz {</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">}</font></div></blockquote><div><br></div><div>In that case, if <font face="Menlo">bar</font> is a non-optional property of <font face="Menlo">foo</font>, I need to rewrite it like this:</div><div><br></div><div></div><blockquote type="cite"><div><font face="Menlo">if let foo = foo, baz = foo.baz {</font></div><div><font face="Menlo"><span style="white-space:pre-wrap">        </span>let bar = foo.bar</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">}</font></div></blockquote><div><br></div><div>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><div><br></div><div><br><div>
<div style="font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><span style="border-collapse:separate;font-family:Helvetica;line-height:normal;border-spacing:0px"><div style="word-wrap:break-word"><div>Jeff Kelley</div><div><br></div><div><a href="mailto:SlaunchaMan@gmail.com" target="_blank">SlaunchaMan@gmail.com</a> | <a href="https://twitter.com/SlaunchaMan" target="_blank">@SlaunchaMan</a> | <a href="http://jeffkelley.org/" target="_blank">jeffkelley.org</a></div></div></span></div></div></div>
</div>
<br><div><blockquote type="cite"><div>On Dec 12, 2015, at 3:58 PM, Zef Houssney via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div>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><br>let col = tableView.columnWithIdentifier("MyColumn")<br>guard let tableView = self.tableView where col != -1 else {<br> NSBeep()<br> print("an error occurred")<br> return<br>}<br><br>Zef<br><br><br><br><blockquote type="cite">On Dec 12, 2015, at 11:23 AM, Al Skipp via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br>How about extending ‘NSTableView’ with a subscript?<br><br>extension NSTableView {<br> subscript(columnID columnID: String) -> Int? {<br> get {<br> let c = columnWithIdentifier(columnID)<br> return c >= 0 ? c : .None<br> }<br> }<br>}<br><br>tableView[columnID: "MyColumn"]<br><br><br>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><br>Al<br><br><blockquote type="cite">On 12 Dec 2015, at 17:43, Jakob Egger via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br>At the moment "guard let" can only be used for unwrapping optionals. It<br>would be really nice if it could also be used with non-optional values.<br>For example, I'd like to write code like the following<br><br>guard <br> let tableView = self.tableView,<br> let col = tableView.columnWithIdentifier("MyColumn") where col != -1<br>else {<br> NSBeep()<br> print("an error occurred")<br> return<br>}<br><br>This is not possible, because the second let assignment is non-optional,<br>so I have to write it like this:<br><br>guard let tableView = self.tableView else {<br> NSBeep()<br> print("an error occurred")<br> return<br>}<br>let col = tableView.columnWithIdentifier("MyColumn")<br>guard col != -1 else {<br> NSBeep()<br> print("an error occurred")<br> return<br>}<br><br>This leads to a lot of duplicated error handling code.<br><br>Ideally, I'd also like to mix let & where clauses in a single guard<br>statement, like this:<br><br>guard <br> let tableView = self.tableView,<br> let col = tableView.columnWithIdentifier("MyColumn") where col !=<br> -1,<br> let headerCell = tableView.tableColumns[col].headerCell as?<br> MyTableHeaderCell<br>else {<br> NSBeep()<br> print("an error occurred")<br> return<br>}<br><br>What do you think? Right now I always end up writing a lot of separate<br>guard statement, and I have a lot of repeated error handling code.<br><br><br>Best regards,<br>Jakob<br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></blockquote><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></blockquote><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></div></blockquote></div><br></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="min-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">
</div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div></div></div></div></div></div><div style="word-wrap:break-word"><div><div><div><div><div><blockquote type="cite"></blockquote></div><br></div></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=QOj-2FXWMwrtGhHdk0ao-2FYqYPqIFSXRT0DG7Sea-2BCcxJOK3R2aZcfGSnrD0z-2Bbt0fOqswnVyEV8X1RZPQ9X-2F-2BJF-2BYufVIFK0SqqFutTpxizt-2FbNeEjuOG9Rj0Rw-2F60kU-2BA57k4UWTN-2B2sZLFNQfvmEop9iDpvBoXQ-2BIZ9jSO4anWU9vqW8-2Fb9FiAiJVhgIcSDbjlqMtRVE9PeTqOUDz6AoyQ0HtOaBEkPwuMhIaAzQWaI-3D" alt="" width="1" height="1" border="0" style="min-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">
</div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><div dir="ltr">-- <br></div><div dir="ltr">Best,<div><br></div><div>Arthur / Sabintsev.com</div></div>