<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="">In thinking about this, is there a way to have the compiler detect that something is a statement vs expression? Is it always clear to users when something is an expression vs a statement? In blurring the lines between expressions and statements complications arise anytime there is a return value returned by the result of “switch” or “if” it suddenly becomes an expression. Are there gray areas where it is hard to determine whether it is one vs the other when we use the those keywords? If it is not possible to determine for every situation or confusing to users, then maybe a new keyword for expressions is necessary. When a branch of an else returns void does it then become a statement? We should avoid shoehorning it in just to avoid another keyword.&nbsp;<div class=""><div class=""><br class=""></div><div class="">let foo = if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x = 1 // implied void -- illegal</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>10 &nbsp;// returns expression</div><div class="">}</div><div class=""><br class=""></div><div class="">also, I think this is confusing:</div><div class=""><br class=""></div><div class="">let foo = if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function1()</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function2()</div><div class="">}</div><div class=""><br class=""></div><div class="">it is not obvious that this is an assignment or that the functions return anything. Earlier I suggested something using = &nbsp;to make it more clear. This is similar to the requirement that functions that throw are prefixed with with “try"</div><div class=""><br class=""></div><div class="">let foo if condition {</div><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;function1()</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;function2()</div><div class="">}</div></div><div class=""><br class=""></div><div class="">also for the literal case:&nbsp;</div><div class=""><br class=""></div><div class=""><div class=""><div class="">let foo &nbsp;if condition {</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;&nbsp;1 // can’t accidentally put a non expression here.&nbsp;</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;10 &nbsp;// returns expression</div><div class="">}</div></div></div><div class=""><br class=""></div><div class="">Which makes it clear that it is an expression coming back from both sides of the “if”. The switch case is a little trickier because of the case.&nbsp;</div><div class=""><br class=""></div><div class=""><div class="">let foo switch value {</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;.Red:&nbsp; &nbsp;function1()</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>=&nbsp;.Green &nbsp;function2()&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>= &nbsp;default: function3()</div><div class="">}</div></div><div class=""><br class=""></div><div class="">The equal emphasizes the functions return a value for each part of the switch and assigns to “foo”, but somehow is unsatisfying having that equal everywhere.&nbsp;</div><div class=""><br class=""></div><div class="">One the other hand, &nbsp;the ternary operator being an expression this confusion does not exist, it is clear that function1() and function2() must return a value.&nbsp;</div><div class=""><br class=""></div><div class="">let foo = condition ? function1() : function2()&nbsp;</div><div class=""><br class=""></div><div class="">even when on separate lines:</div><div class=""><br class=""></div><div class="">let foo = condition ?&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function1() :</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function2()&nbsp;</div><div class=""><br class=""></div><div class="">So maybe Alex’s original suggestion works better where the ? operator is extended to support a switch like expression and keeping the two statements and expressions separate.&nbsp;</div><div class=""><br class=""></div><div class="">let foo = condition ?&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.Red : .Green</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.Green : Red</div><div class=""><br class=""></div><div class="">let foo = condition ?&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.Red: function1()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.Green: function2()&nbsp;</div><div class=""><br class=""></div><div class=""><div class="">let foo = condition ?&nbsp;.Red: function1() .Blue: function2() default:. function3()&nbsp;</div></div><div class=""><br class=""></div><div class="">also could include optional cases:</div><div class=""><br class=""></div><div class="">let foo = condition ? case .Red: function1(), case .Blue: functions(), default: function3()</div><div class=""><br class=""></div><div class="">Which brings us back full circle to the keyword because most people don’t like the ? operator which is why Alex suggested “match":</div><div class=""><br class=""></div><div class=""><div class="">let foo = match condition&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>.Red: function1()</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>.Green: function2()&nbsp;</div></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>default: function3()</div><div class=""><br class=""></div><div class="">or with optional cases:&nbsp;</div><div class=""><br class=""></div><div class=""><div class=""><div class="">let foo = match condition&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case&nbsp;.Red: function1()</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case&nbsp;.Green: function2()&nbsp;</div></div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>default: function3()</div></div><div class=""><br class=""></div><div class="">for booleans :</div><div class=""><br class=""></div><div class="">let too = match condition function() else function2()</div><div class=""><br class=""></div><div class="">I still like this better. A new keyword makes sure there is no confusion about expressions vs statements and avoids complications with the return values. Match would always be an expression, if/else/swtich are always statements. Also those keywords don’t change behavior if a user changes a statement into an expression by assigning the expression the else part would suddenly be required.&nbsp;</div><div class=""><br class=""></div><div class="">if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function1()</div><div class="">}&nbsp;</div><div class=""><br class=""></div><div class="">is changed to&nbsp;</div><div class=""><br class=""></div><div class="">let foo = if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>function1() &nbsp;</div><div class="">} &nbsp;</div><div class=""><br class=""></div><div class="">is now an error, because there is no else. Now if function1() does not return a value. you have another error just changing it to assign completely changes the behavior of the “if” statement.&nbsp;</div><div class=""><br class=""></div><div class="">- Paul</div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Dec 6, 2015, at 2:11 PM, Paul Ossenbruggen &lt;<a href="mailto:possen@gmail.com" class="">possen@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="">so the rule would have to be that the “switch" or “if" must return a value of compatible type. Not sure I love all the braces in the “if" case but it does follow the existing language rules with the exception that it must be have an else.<div class=""><br class=""></div><div class="">let thisColor = if condition { .Red } &nbsp; // illegal for expressions but not statements</div><div class=""><br class=""></div><div class="">Can still do this for statements:</div><div class="">if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x = 40</div><div class="">}<br class=""><div class=""><br class=""></div><div class="">likewise:</div><div class="">let thisColor = if condition { .Red } else { 21 } // illegal unless thisColor is Any&nbsp;</div><div class=""><br class=""></div><div class="">unless:&nbsp;</div><div class=""><div class="">let thisColor : Any = if condition { .Red } else { 21 } // illegal unless thisColor is Any&nbsp;</div></div><div class=""><br class=""></div><div class="">It would be nice to omit braces in this expression case but not for statements:&nbsp;</div><div class="">let thisColor = if condition .Red else .Blue&nbsp;</div><div class=""><br class=""></div><div class="">in statements braces would be required:&nbsp;</div><div class=""><br class=""></div><div class="">if condition {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x = 32</div><div class="">} else {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>y = 44</div><div class="">}</div><div class=""><br class=""><blockquote type="cite" class=""><div class="gmail_extra"><div class="gmail_quote"><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;"></blockquote><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;"></blockquote><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;"></blockquote><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;"><span class="">&gt;&nbsp; &nbsp; &nbsp;}</span></blockquote></div></div></blockquote><div class=""><blockquote type="cite" class=""><div class="">On Dec 6, 2015, at 1:52 PM, Alex Lew 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="">I agree that it's simplest to just reuse switch keyword, and keep braces. +1. &nbsp;<div class=""><br class=""></div><div class="">Would you allow the same thing with if?<div class=""><br class=""></div><div class="">let thisColor = if condition { .Red } else { .Blue }</div></div></div><div class="gmail_extra" 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;"><br class=""><div class="gmail_quote">On Sun, Dec 6, 2015 at 4:44 PM, Rudolf Adamkovic<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:salutis@me.com" target="_blank" class="">salutis@me.com</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><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;"><span class="">&gt; On 06 Dec 2015, at 22:35, thorsten--- via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">&gt;<br class="">&gt; I would prefer the expression to match the statement. The only difference would be that all parts that were statements now have to be expressions.<br class=""><br class=""></span>+1<br class=""><span class=""><br class="">&gt;<br class="">&gt; Therefore the switch-expression should simply look like follows:<br class="">&gt;<br class="">&gt; let thisColor = switch thatColor {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case .Red: .Green // must be an expression<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default: .Yellow&nbsp; &nbsp; &nbsp; // must be an expression<br class="">&gt;&nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt; No returns needed in the case clauses.<br class=""><br class=""></span>This actually looks great. One simple rule and zero new keywords.<br class=""><br class="">Readable and simple to learn.<br class=""><br class="">Fantastic!<br class=""><span class="im HOEnZb"><br class="">&gt; Formatting this as a one-liner would just require adding semicolons (though I wouldn't recommend this).<br class="">&gt;<br class="">&gt; -Thorsten<br class=""></span><div class="HOEnZb"><div class="h5">&gt; _______________________________________________<br class="">&gt; swift-evolution mailing list<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><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=""></div></div></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=emrIhnP1hIf76Foxxv4NNJQX-2FWhcznESwKBSwD1MEwyP5CIr9PwUjY-2Ffl0vJP84s2E-2BPNmnmOU7FHbsypgjBVsA6XXVEIgefBIcq4-2BKrD6hXzZp9CEkFf-2FSAQvGZbuSiTLYT2uZ5LCc8U8KcITFXfDZ60W2ZWl5smFk598oozibBFsQxksuR4PHnB7FZ4Ohsb5TV78p5qHsJ7zZPfQPLXA-3D-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></div></div></blockquote></div><br class=""></div></div></body></html>