<div style="white-space:pre-wrap">+1 on default return <br><br>-1 on default continue or break, this is ambiguous. <br>Even inside switch it's not clear if guard should break or return, so let's not make people guess. . <br><br>Also can we stop requiring braces for simple one-liners: <br><br>guard x<10 else return 5<br><br>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><br>> swift would provide a warning that the guard statement needs an else block<br><br>In this specific case the compiler basically has to guess, so an error seems more appropriate. <br><br>Ilya. </div><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 16, 2015 at 02:53 Vester Gottfried 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 dir="ltr"><font face="monospace, monospace">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><br>In a current project I found 217 guard statements from which 183 have repetitive else clauses<br><br>From which:<br>131 end with "else { return nil }"<br>44 "else { return }"<br>6 "else { continue }"<br>2 "else { break }"<br><br>My proposal would be to make the else block optional and define a default behaviour.<br><br>For example:<br><br>func foo(x: Int) {<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>guard x < 10<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>...<br>}<br><br>swift would implicitly add "else { return }"<br><br>--<br><br>func foo(x: Int) -> Int? {<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>guard x < 10<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>...<br>}<br><br>swift would implicitly add "else { return nil }"<br><br>--<br><br>for i in 0..<10 {<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>guard i%2 == 0<br>}<br><br>swift would implicitly add "else { continue }"<br><br>--<br><br>switch {<br>case a :<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>guard x != y<br>case b :</font><div><font face="monospace, monospace"><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> ...</div><br>}<br><br>swift would implicitly add "else { break }"<br><br>--<br><br>func foo(x: Int) -> Int {<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>guard x < 10<br><div class="gmail_default" style="font-family:monospace,monospace;display:inline"> </div>...<br>}<br><br>swift would provide a warning that the guard statement needs an else block<br><br>--<br><br>Possible advantages<br>- Less code<div class="gmail_default" style="font-family:monospace,monospace;display:inline"> to write</div><br>- visually cleaner<br>-<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><br><br>Possible Disadvantages<br>- Different behaviour in different contexts (func/return, for/continue, switch/break, …) needs to be learned and understood<br>- programmers might forget that guard + else {} is an option</font><br></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: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">
_______________________________________________<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>