<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="">If guard defaults to something context-dependent, it's also counterintuitive.<div class=""><br class=""></div><div class=""><br class=""></div><div class="">Me personally is fine with guard x &gt; 0 returning from the function in this case.</div><div class=""><br class=""></div><div class="">I use guard mainly as a glorified assert that allows safe exit from the function instead of crashing the program on fail.</div><div class="">If you think about it that way, it's perfectly reasonable that it returns in all cases.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">On the other hand, the implicit default else behaviour could only trigger if there is no outer scope that can be exited with break.</div><div class="">Maybe OP could post additional statistics of the number cases where "guard ... else { return }" is used inside a breakable scope.</div><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Etan</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 16 Dec 2015, at 23:15, Vinicius Vendramini &lt;<a href="mailto:vinivendra@gmail.com" class="">vinivendra@gmail.com</a>&gt; wrote:</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=""><div class="">Bringing up a possible edge case:</div><div class=""><br class=""></div><div class="">func foo() {</div><div class="">&nbsp; &nbsp; while(…) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; guard x &gt; 0</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">if guard defaulted to return even inside a while (as was suggested), this might be counterintuitive.</div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 16, 2015, at 5:06 PM, Etan Kissling via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">



<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Also +1 on default return, -1 on default continue / break, and -1 for removing braces
<div class=""><br class="">
</div>
<div class="">Instead of VoidLiteralConvertible, one could extend on the idea with something that is not specifically tailored to nil, like</div>
<div class=""><br class="">
</div>
<div class="">func foo(x: Int) -&gt; Int = 5 {</div>
<div class="">&nbsp; &nbsp; guard x &lt; 10 // Would return default 5 for x &gt;= 10</div>
<div class=""><br class="">
</div>
<div class="">&nbsp; &nbsp; if x &gt; 5 {</div>
<div class="">&nbsp; &nbsp; &nbsp; &nbsp; return // Would return default 5</div>
<div class="">&nbsp; &nbsp; }</div>
<div class="">&nbsp; &nbsp; return x</div>
<div class="">}</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Etan</div>
<div class="">
<div class=""><br class="">
</div>
<div class=""><br class="">
<div class="">
<blockquote type="cite" class="">
<div class="">On 16 Dec 2015, at 19:39, Ian Ynda-Hummel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">
<div class="">I am also&nbsp;+1 for implicit return, but -1 on continue/break for the reasons already stated.</div>
<div class=""><br class="">
</div>
I'm -1 for removing braces for one-liners. I think maintaining braces around blocks helps distinguish them from expressions. For example,
<div class=""><span class="s1" style="line-height: 1.5;"><br class="">
</span></div>
<div class=""><span class="s1" style="line-height: 1.5;">&nbsp; &nbsp; guard</span><span class="s2" style="line-height: 1.5;"><span class="Apple-converted-space">&nbsp;</span>x &lt; 1</span><span class="s3" style="line-height: 1.5;">0</span><span class="s2" style="line-height: 1.5;"><span class="Apple-converted-space">&nbsp;</span></span><span class="s1" style="line-height: 1.5;">else</span><span class="s2" style="line-height: 1.5;"><span class="Apple-converted-space">&nbsp;</span></span><span class="s1" style="line-height: 1.5;">return</span><br class="">
</div>
<div class=""><span class="s1" style="line-height: 1.5;"><br class="">
</span></div>
<div class=""><span class="s1" style="line-height: 1.5;">would catch me off guard (pardon the pun).</span></div>
<div class=""><span class="s1" style="line-height: 1.5;"><br class="">
</span></div>
<div class="">I think I'm -1 on VoidLiteralConvertible, but I'm somewhat undecided. I think that separating the return value from the actual return point could lead to a lot of confusion, and would subsequently also make it easy to accidentally return the default
 value when you didn't intend to as the compiler wouldn't complain about a missing return value.&nbsp; I don't think I have totally convinced myself that the latter is a non-trivial problem, but I thought it was worth mentioning.</div>
</div>
<br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">
<div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;">
<div dir="ltr" class="">On Wed, Dec 16, 2015 at 11:59 AM ilya via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">
<div style="white-space: pre-wrap;" class="">Actually I thought about VoidLiteralConvertible some more and now I think if we include it the only nontrivial case in the standard library case should be Optional.Nonel. Empty arrays and dictionaries are different
 from nothing, so it's best to always return them explicitly. <br class="">
<br class="">
Oh, and it would help with default values, e.g.<br class="">
<br class="">
var something:Something?<br class="">
// where does the default value come from? VoidLiteralConvertible! <br class="">
<br class="">
I want all default values for String in this scope be "none":<br class="">
<br class="">
private extension String: VoidLiteralConvertible { ... return "None" ... }</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="">On Wed, Dec 16, 2015 at 10:49 ilya &lt;<a href="mailto:ilya.nikokoshev@gmail.com" target="_blank" class="">ilya.nikokoshev@gmail.com</a>&gt; wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">
<div style="white-space: pre-wrap;" class="">+1 on default return <br class="">
<br class="">
-1 on default continue or break, this is ambiguous. <br class="">
Even inside switch it's not clear if guard should break or return, so let's not make people guess. .
<br class="">
<br class="">
Also can we stop requiring braces for simple one-liners: <br class="">
<br class="">
guard x&lt;10 else return 5<br class="">
<br class="">
As for default return values, we could create a VoidLiteralConvertible, so that default return automatically becomes return nil or return [] in an Optional or Array context respectively. As a bonus, it will be technically possible to override this behavior
 inside a specific function scope. <br class="">
</div>
<div style="white-space: pre-wrap;" class=""><br class="">
&gt; swift would provide a warning that the guard statement needs an else block<br class="">
<br class="">
</div>
<div style="white-space: pre-wrap;" class="">In this specific case the compiler basically has to guess, so an error seems more appropriate.
<br class="">
</div>
<div style="white-space: pre-wrap;" class=""><br class="">
Ilya. </div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="">On Wed, Dec 16, 2015 at 02:53 Vester Gottfried via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">
<div dir="ltr" class=""><font face="monospace, monospace" class="">I find myself writing the same else blocks for guard statements over and over again, so it might be reasonable to think about a default behaviour.<br class="">
<br class="">
In a current project I found 217 guard statements from which 183 have repetitive else clauses<br class="">
<br class="">
From which:<br class="">
131 end with "else { return nil }"<br class="">
44 "else { return }"<br class="">
6 "else { continue }"<br class="">
2 "else { break }"<br class="">
<br class="">
My proposal would be to make the else block optional and define a default behaviour.<br class="">
<br class="">
For example:<br class="">
<br class="">
func foo(x: Int) {<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
guard x &lt; 10<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
...<br class="">
}<br class="">
<br class="">
swift would implicitly add "else { return }"<br class="">
<br class="">
--<br class="">
<br class="">
func foo(x: Int) -&gt; Int? {<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
guard x &lt; 10<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
...<br class="">
}<br class="">
<br class="">
swift would implicitly add "else { return nil }"<br class="">
<br class="">
--<br class="">
<br class="">
for i in 0..&lt;10 {<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
guard i%2 == 0<br class="">
}<br class="">
<br class="">
swift would implicitly add "else { continue }"<br class="">
<br class="">
--<br class="">
<br class="">
switch {<br class="">
case a :<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
guard x != y<br class="">
case b :</font>
<div class=""><font face="monospace, monospace" class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;...​</div>
<br class="">
}<br class="">
<br class="">
swift would implicitly add "else { break }"<br class="">
<br class="">
--<br class="">
<br class="">
func foo(x: Int) -&gt; Int {<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
guard x &lt; 10<br class="">
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>&nbsp; &nbsp;​</div>
...<br class="">
}<br class="">
<br class="">
swift would provide a warning that the guard statement needs an else block<br class="">
<br class="">
--<br class="">
<br class="">
Possible advantages<br class="">
- Less code
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​<span class="Apple-converted-space">&nbsp;</span>to write​</div>
<br class="">
- visually cleaner<br class="">
-
<div class="gmail_default" style="font-family: monospace, monospace; display: inline;">
​ ​</div>
In code with multiple guard statements
<div class="gmail_default" style="display: inline;">​ ​</div>
you
<div class="gmail_default" style="display: inline;">​ ​</div>
would not have to repeat the else block<br class="">
<br class="">
<br class="">
Possible Disadvantages<br class="">
- Different behaviour in different contexts (func/return, for/continue, switch/break, …) needs to be learned and understood<br class="">
- programmers might forget that guard + else {} is an option</font><br class="">
</div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54Nq6HB1QvDr1NATTiNVHM5FeRnZlEYkvBM4a9SOK9uUTg-2BKC5LbuPWspn7WIdvJStDdL7cEklJGmF7ns38m-2FKlGgd9MjsXwAa1k0rLKghHgarLpkoGqHTF9JIDwQ2YLp79pV5tTFvxeEMB0tN6N7G9ck9PoGgGpWWLOInd26COTyoubf2xkRWCqHceU0vu4cVQ-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span class="Apple-converted-space">&nbsp;</span>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote>
</div>
</blockquote>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=t5fkNsX-2F7-2FAygTFNPNE08PALgkjkO6I8wpsdssDS0ujZY5P9vBLfqz65hb0zEcOZ2tVKrT5CpPLK-2BDFckSfHeJRQIAc4R5G5miuYly6lTh7kICRixRvR73QdPQe4GS3yI9dMOVt4bJ1hZ-2FCJQYsofdD4Ve6YA75oUM9SYzZJP-2Bycbbm-2BuEDzxQ7K9Cmc7Ym7oFezb-2BWxnpAND1s-2BSX-2Bh-2FJXCk1KlXO8nxizWqzum-2FSg-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span class="Apple-converted-space">&nbsp;</span>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=tTTJ5sn5y0uc3ODSZa-2BndLNwXCDS7T2cq5OlDDhG0Rso4-2BeBmpFqe52uiIPfiElqhfE1eQ-2FPzaEev9pw-2B0Cz79UMrllX95ALM-2F816LC2lQLfktGsolsjperzaTBVJ2XGOreI5CVQvjKJjeTlsU69rwxjEql-2BQuZYaJHfOxmx9RCw7GDc1NNB395q6JAqO5-2BHePxRL-2BH9kjEgDsGce45LeK9Lb6w0EfyfqzSlDZ-2FXsIY-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">
<span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; float: none; display: inline !important;" class="">swift-evolution
 mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">
<a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div>
</blockquote>
</div>
<br class="">
</div>
</div>

<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=LFwSuj9FMv8p5lHRk78JpoTG6TdqPOb6ZXyNv3YIC3z6TWrqtMgNCUrw18WowcmWNvj-2BcGK4ulK3T1X-2B1gbwKOiBlqRpjaZl0A-2FigogKyqom-2F50XBi6FKrmTY9ksFC3NhJRUjG-2F8-2F0r73NOP9fvJb9D7hANUIxgBpbzqz7cGzpZU1LpexaPj7G1eUHFbFL0MS41j-2F7LY4eSOVgxoVkspj082qChurPzD6c9OaE7ZmBg-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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></body></html>