<div dir="ltr">There are a couple of languages that use then inside their switches. <div><a href="http://rigaux.org/language-study/syntax-across-languages.html#CntrFlowMltSlcSwt">http://rigaux.org/language-study/syntax-across-languages.html#CntrFlowMltSlcSwt</a><br></div><div><br></div><div>I like using only one then in the switch to signal to the parser that this is a switch expression</div><div><br></div><div>The only issue I see is that the colon in the switch is used in the same way as the current ternary expression. </div><div><br></div><div>Personally I don't think that a switch expression is all that useful since being a multiline statement actually helps the readability. </div><div><br></div><div><span class="">var</span><span class=""> result:</span><span class="">Int</span><span class=""> = {</span><span class="">if</span><span class=""> </span><span class="">bool</span><span class=""> {</span><span class="">return</span><span class=""> </span><span class="">1</span><span class="">} </span><span class="">else</span><span class=""> </span><span class="">if</span><span class=""> </span><span class="">bool </span><span class="">{</span><span class="">return</span><span class=""> </span><span class="">2</span><span class="">} </span><span class="">else</span><span class=""> {</span><span class="">return</span><span class=""> </span><span class="">3</span><span class="">}}()</span><br></div>
<div><span class=""><br></span></div><div><span class="">VS</span></div><div><span class=""><br></span></div><div><span class="">var</span><span class=""> result = </span><span class="">if</span><span class=""> bool then </span><span class="">1</span><span class=""> </span><span class="">else</span><span class=""> </span><span class="">if</span><span class=""> bool then </span><span class="">2</span><span class=""> </span><span class="">else</span><span class=""> </span><span class="">3</span> <br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 12, 2015 at 11:15 AM, Al Skipp via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><div><blockquote type="cite"><div>On 12 Dec 2015, at 14:48, Paul Ossenbruggen <<a href="mailto:possen@gmail.com" target="_blank">possen@gmail.com</a>> wrote:</div><br><div><span style="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;float:none;display:inline!important">Dropping “case” might be interesting, since it becomes a little redundant in the “switch” situation, this is one advantage of having a new keyword, but not sure this reads as well:</span><div style="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"><br></div><div style="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">let v = switch val then .Red: 1, .Green: 2, .Blue: 3</div><div style="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"><br></div><div style="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">It is definitely nice in it’s compactness which is a big plus. </div><div style="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"><br></div><div style="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">Another possibility, because “switch" does not need to resolve the syntactic ambiguity, but then we lose the “then” always meaning an expression consistency. </div><div style="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"><br></div><div style="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">let v = switch val case .Red: 1, case .Green: 2, case .Blue: 3</div><div style="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"><br></div><div style="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">this might be better for switch because we don’t need to mix “then” with “switch” which historically has not been done. Question is, is it better to go with “then” as expression consistency or with slightly more compact and following the conventions out there. Personally, I am not bothered by using “then” with “switch” </div></div></blockquote></div><br></span><div>Would the cases need to be comma separated? Would a new line make more sense instead?</div><div><br></div><div>Currently this is possible:</div><div><br></div><div><div>enum Col { case Red, Green, Blue }</div><div><br></div><div>let c = Col.Blue</div><div><br></div><div>let x: Int = {</div><div> switch c {</div><div> case .Red: return 1</div><div> case .Green: return 2</div><div> case .Blue: return 3</div><div> }</div><div>}()</div></div><div><br></div><div>It works, but there are several things I don’t like:</div><div>- the switch statement must be wrapped in a closure which is invoked at the end.</div><div>- the type of ‘x’ needs to be given</div><div>- each case requires a return statement </div><div><br></div><div>Here’s a proposal for a switch expression:</div><div><br></div><div><div>let y = switch c then {</div><div> .Red: 1</div><div> .Green: 2</div><div> .Blue: 3</div><div>}</div></div><div><br></div><div>I think the braces are probably needed in the switch expression, also the ‘then’ keyword’ does look a bit peculiar. Any other ideas on how to support both switch statements and expressions?</div><div><br></div><div>Al</div><div><br></div><div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OWK4tSasaK2n-2FQIIcS9Ug-2FuFXG-2BJ3z6cFMLgm306hDcwXSrBgwAhVcBB1bD-2BWBAOrMiWlYEMCLorIawg102oISeXQ6BBqHqegKWQ96WdP0km5l6YPmRJFAsrMBq1i-2BKQ46yyZKJzbH8ZoPF0S74y9jEx-2FgfUmcfN6k36LyKEXahLaLdAaZwETBqfRz9G3R26OqFrTL8LcpoLTfVc-2FY-2FLBt9zdihYpasX9V-2FYk9wYsJg-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>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
<br></blockquote></div><br></div>