<div dir="ltr"><div>We could do a Ruby and return the last value seen in a swift block. :) <br></div><div><br></div><div>let a = switch(value)</div><div>{</div><div> case is Int:</div><div> "Frodo"</div><div>}</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 6, 2016 at 2:01 PM, Charles Constant 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 dir="ltr">I can't stop fiddling around with this... so here's another minor variation. My new new favourite, if we're doubling down on the ternary. <div><br></div><div>We could start with Paul's ternary example... but it seems more in keeping to me, with the existing ternary, to use a colon as a separator. If we do that, an exclamation point seems appropriate, and pretend that it's just how a ternary expression behaves when it's testing something extra-ternary (like a n enum with four possible cases)<div><br></div><div><p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;color:rgb(146,146,146)"><span style="color:rgb(34,34,34);white-space:pre-wrap">        </span>// Syntax when the value you’re testing is a Boolean</p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;color:rgb(0,97,255)"><span style="color:rgb(34,34,34);white-space:pre-wrap">        </span><span style="color:rgb(0,0,0)">let </span><span style="color:rgb(227,36,0)">val</span><span style="color:rgb(0,0,0)"> = </span><span style="color:rgb(102,156,53)">boo</span><span style="color:rgb(0,0,0)"> ? </span>0xFF0000<span style="color:rgb(0,0,0)"> : </span>0x00FF00</p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;min-height:15px"><br></p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;color:rgb(146,146,146)"><span style="color:rgb(34,34,34);white-space:pre-wrap">        </span>// Syntax when the value you’re testing is not Boolean</p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial"><span style="white-space:pre-wrap">        </span>let <span style="color:rgb(227,36,0)">val</span> = <span style="color:rgb(102,156,53)">color</span> ? ( <span style="color:rgb(153,41,189)">.Red</span> ! <span style="color:rgb(0,97,255)">0xFF0000</span> ) : ( <span style="color:rgb(153,41,189)">.Green</span> ! <span style="color:rgb(0,97,255)">0x00FF00</span> ) : ( <span style="color:rgb(153,41,189)">.Blue</span> ! <span style="color:rgb(0,97,255)">0x0000FF</span> ) : ( <span style="color:rgb(153,41,189)">_</span> ! <span style="color:rgb(0,97,255)">0xFFFFFF</span> )</p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;min-height:15px"><br></p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;color:rgb(146,146,146)"><span style="color:rgb(34,34,34);white-space:pre-wrap">        </span>// … Parens are not required, but probably unwise unless you format on multiple lines. Eg:</p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial"><span style="white-space:pre-wrap">        </span>let <span style="color:rgb(227,36,0)">val</span> = <span style="color:rgb(102,156,53)">color</span> ? </p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial"><span style="white-space:pre-wrap"><span>        </span>        </span><span style="color:rgb(153,41,189)">.Red</span> ! <span style="white-space:pre-wrap">        </span><span style="color:rgb(0,97,255)">0xFF0000</span> : </p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial"><span style="white-space:pre-wrap"><span>        </span>        </span><span style="color:rgb(153,41,189)">.Green</span> ! <span style="white-space:pre-wrap">        </span><span style="color:rgb(0,97,255)">0x00FF00</span> : </p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial"><span style="white-space:pre-wrap"><span>        </span>        </span><span style="color:rgb(153,41,189)">.Blue</span> ! <span style="white-space:pre-wrap">        </span><span style="color:rgb(0,97,255)">0x0000FF</span> : </p>
<p style="margin:0px;font-size:13px;line-height:normal;font-family:Arial;color:rgb(0,97,255)"><span style="color:rgb(34,34,34);white-space:pre-wrap">        </span><span style="color:rgb(0,0,0)"><span style="white-space:pre-wrap">        </span></span><span style="color:rgb(153,41,189)">_</span><span style="color:rgb(0,0,0)"> ! <span style="white-space:pre-wrap">                </span></span>0xFFFFFF</p></div><div><br></div><div>I actually like this a lot because it's concise, and aesthetically in harmony with the normal ternary.</div><div><br></div><div>There's obvious downsides to using an exclamation point in Swift, but I think it's still pretty nice.</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 5, 2016 at 11:41 PM, Paul Ossenbruggen 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: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="word-wrap:break-word">Heh, one other thought. Pretty sure this was rejected by the switch statement but if keeping it concise for expressions is a goal then _ might make sense for the default case especially with this form. :-) <div><br></div><div><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font face="Menlo"> fh = ?(</font><span style="font-family:Menlo;color:rgb(79,129,135)">color,</span><font face="Menlo"> .Red: </font><font color="#272ad8" face="Menlo">0xFF0000 </font><font face="Menlo" color="#669c35">// only one expression can be the result here so case is unnecessary.</font><font color="#272ad8" face="Menlo"> </font><br><span style="font-family:Menlo"> .Green:</span><span style="font-family:Menlo"> </span><span style="font-family:Menlo;color:rgb(39,42,216)">0x00FF00</span><br><span style="font-family:Menlo"> .Blue:</span><span style="font-family:Menlo"> </span><span style="font-family:Menlo;color:rgb(39,42,216)">0x0000FF</span><br><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal;font-family:Menlo"> _: <span style="color:rgb(39,42,216)">0xFFFFFF</span>) <font color="#669c35">// default is always last, unless all cases are handled.</font> </div></div></div></div><div><div style="word-wrap:break-word"><div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><br></div></div></div></div></div><div>I agree with it being rejected for the statement form, but kinda makes sense here. </div><div><br><blockquote type="cite"><div>On Jan 5, 2016, at 10:50 PM, Paul Ossenbruggen <<a href="mailto:possen@gmail.com" target="_blank">possen@gmail.com</a>> wrote:</div><br><div><div style="word-wrap:break-word">The thing I don’t like is the repeated use of case. and default. This, I think makes the expression longer than it needs to be. For expressions it is not necessary because you only have one expression which returns one value, where as in a statement you have multiple statements following it, so it is necessary to separate the cases with the word “case”. I think trying to reduce clutter is important. <div><br></div><div>For example, statements have to deal with multiple statements per case. </div><div><br></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> color = <span style="color:rgb(79,129,135)">Colors</span>.<span style="color:rgb(49,89,93)">Red</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">// hello</div><span><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> res : <span style="color:rgb(112,61,170)">Int</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">switch<span> </span><span style="color:rgb(79,129,135)">color</span><span> {</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> .Red:</div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>res = <span style="color:rgb(39,42,216)">0xFF0000</span></div></span><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(39,42,216)"><span style="white-space:pre-wrap">        </span>print(res) // case is necessary here to separate statement lists. </span></div><span><div style="margin:0px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> .Green: </div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(79,129,135)"><span style="white-space:pre-wrap">        </span>res</span> = <span style="color:rgb(39,42,216)">0x00FF00</span></div></span><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(39,42,216)"><span style="white-space:pre-wrap">        </span>print(res)</span></div><span><div style="margin:0px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">case</span> .Blue: </div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(79,129,135)"><span style="white-space:pre-wrap">        </span>res</span> = <span style="color:rgb(39,42,216)">0x0000FF</span></div></span><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(39,42,216)"><span style="white-space:pre-wrap">        </span>print(res)</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">default<span>:</span></div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(39,42,216)"><span> </span><span style="color:rgb(79,129,135)">res</span><span> = </span>0xFFFFFF</div><div style="margin:0px;line-height:normal;font-family:Menlo">}</div><div><br></div><div>This is why I went down the path of trying to group it with parenthesis and optionally remove the word case. I know it may seem a little less like a "switch" but I think having to write out “case" over and over is not great when it does not serve the same purpose as it does in the statement form. So in the compact form: </div><div><br></div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font face="Menlo"> fh = ?(</font><span style="font-family:Menlo;color:rgb(79,129,135)">color,</span><font face="Menlo"> .Red: </font><font color="#272ad8" face="Menlo">0xFF0000 </font><font face="Menlo" color="#669c35">// only one expression can be the result here so case is unnecessary.</font><font color="#272ad8" face="Menlo"> </font></div><div style="margin:0px;line-height:normal;font-family:Menlo"> .Green: <span style="color:rgb(39,42,216)">0x00FF00</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> .Blue: <span style="color:rgb(39,42,216)">0x0000FF</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> : <span style="color:rgb(39,42,216)">0xFFFFFF</span>) <font color="#669c35">// default is always last, unless all cases are handled.</font> </div></div><div><br></div><div><br></div><div>This seems more light and more and just as understandable as Thorsten's suggestion. (Note: I keep playing with the separators because the feedback was, my latest suggestions were not Swift like). Also, it is possible to drop the “case" in this structure and it can be read more like a function if desired. My suggestion also supported this form but its only advantage is that it looks more like a “switch", there may be value in that but I think conciseness and less clutter should win:</div><div><br></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> fh = ?(<span style="color:rgb(79,129,135)">color, case</span> .Red: <span style="color:rgb(39,42,216)">0xFF0000</span></div><span><div style="margin:0px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">case</span> .Green: <span style="color:rgb(39,42,216)">0x00FF00</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> <span style="color:rgb(79,129,135)">case</span> .Blue: <span style="color:rgb(39,42,216)">0x0000FF</span></div></span><div style="margin:0px;line-height:normal;font-family:Menlo"> <font color="#4f8187">default</font>: <span style="color:rgb(39,42,216)">0xFFFFFF</span>)</div></div><div><br></div><div>Not sure though if people don’t like having alternatives forms though.</div><div><br></div><div>I think there is value in having a consistent way of doing index based as well as boolean based expressions as I have suggested in my proposal. I know that is a harder thing to push for but I think it is a win. I have backpedaled on some of my weirder character choices here so it more like the ternary. </div><div><br></div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font face="Menlo"> fb = ?(pickOne, </font><font color="#d12f1b" face="Menlo">“A",</font><font face="Menlo"> </font><span style="font-family:Menlo;color:rgb(209,47,27)">"B"</span><font face="Menlo">, </font><span style="font-family:Menlo;color:rgb(209,47,27)">"C"</span><font face="Menlo">, </font><span style="font-family:Menlo;color:rgb(209,47,27)">"D"</span><font face="Menlo">, </font><span style="font-family:Menlo;color:rgb(209,47,27)">"E"</span><font face="Menlo">, </font><span style="font-family:Menlo;color:rgb(209,47,27)">"F"</span><font face="Menlo">, </font><span style="font-family:Menlo;color:rgb(209,47,27)">"G</span><font face="Menlo"><font color="#d12f1b">”</font> : </font><span style="font-family:Menlo;color:rgb(209,47,27)">"Z"</span><font face="Menlo">)</font></div></div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font color="#008400" style="font-family:Menlo"> fe = ?(truthy == truth, </font><font color="#272ad8"><font face="Menlo">“unlikely</font></font><font><font color="#272ad8" face="Menlo">” :</font><font face="Menlo" color="#008400"> </font></font><font color="#272ad8" face="Menlo">“likely"</font><font color="#008400" style="font-family:Menlo">)</font></div><div style="font-family:Menlo;color:rgb(0,132,0)"><br></div></div></div><div>If you look at these suggestions above, I have only moved one ? added a comma, and added parenthesis to the ternary. I think these latest suggestions look more like Swift and more like the ternary than my last email.</div><div><br></div><div>If it is really preferred the control value could be moved outside the parens but I think it makes it harder to find the beginning and end of the ternary and it does not have the advantage of the function like feel to it:. </div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font color="#008400" style="font-family:Menlo"> fe = </font><span style="color:rgb(0,132,0);font-family:Menlo">truthy == truth </span><font color="#008400" style="font-family:Menlo">?(</font><font color="#272ad8"><font face="Menlo">“unlikely</font></font><font color="#272ad8" face="Menlo">” :</font><font face="Menlo" color="#008400"> </font><font color="#272ad8" face="Menlo">“likely"</font><font color="#008400" style="font-family:Menlo">)</font></div></div><div><font color="#008400" style="font-family:Menlo"><br></font></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> fh = <span style="color:rgb(79,129,135)">color </span>?( .Red: <span style="color:rgb(39,42,216)">0xFF0000</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> .Green: <span style="color:rgb(39,42,216)">0x00FF00</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> .Blue: <span style="color:rgb(39,42,216)">0x0000FF</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> : <span style="color:rgb(39,42,216)">0xFFFFFF</span>) </div></div><div><br></div><div>finally we could drop the parenthesis all together and the ternary is back in original form, which results in Thorsten’s suggestion minus the cases,</div><div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;color:rgb(187,44,162)">let</span><font color="#008400" style="font-family:Menlo"> fe = </font><span style="color:rgb(0,132,0);font-family:Menlo">truthy == truth </span><font color="#008400" style="font-family:Menlo">? </font><font color="#272ad8"><font face="Menlo">“unlikely</font></font><font color="#272ad8" face="Menlo">” :</font><font face="Menlo" color="#008400"> </font><font color="#272ad8" face="Menlo">“likely"</font></div></div></div><div><font color="#008400" style="font-family:Menlo"><br></font></div><div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> fh = <span style="color:rgb(79,129,135)">color </span>? .Red: <span style="color:rgb(39,42,216)">0xFF0000</span></div><span><div style="margin:0px;line-height:normal;font-family:Menlo"> .Green: <span style="color:rgb(39,42,216)">0x00FF00</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> .Blue: <span style="color:rgb(39,42,216)">0x0000FF</span></div><div style="margin:0px;line-height:normal;font-family:Menlo"> : <span style="color:rgb(39,42,216)">0xFFFFFF</span> </div></span></div></div><div><br></div><div>I think the parenthesis and the control inside the construct really help and this may be hard to parse without the grouping the parenthesis provides. I don’t think we are that far apart though. </div><div><br></div><div>- Paul </div><div><div><div><br></div></div></div></div></div></div></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=l3fs1g-2F466j3y5fD5Q61KddsTTCmXL0uxw3XoAMFFNgU0tX6c1ofiXFbZlOFKogd7CeXqZEaaCYoXd8hHh8k6rfo-2BRTd643OJAY-2FgKuzZWdMm-2BBnX5f46nzzL8zsttqNfilyOPNCp5XJJOKNt5rrnH-2F18sfxJYn8Ww2DPnOppfLKkt1eRxCIrLmRLhJWLS9ujSppxJpv2ltGUhMoRUeRhisCg-2BrgSPfWdscpgL82ew0-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">
</div>
<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=xV0JY-2FdZMnUMvSFtZnLiBPRTDDOSQf3-2FpH33HYOlBxE5Y6euMyAJE5QEpoIbCAp-2Ft6pyYYGsF-2B10fRv-2BRX7bBtIhFk3Zj84QjBnFaSOpxYNYkJZg-2Frh2SyW-2BZ4Dc3qLVXz8OCkfu-2Bk2-2BLFYwFMZsTkaiBkAXfE7tEJZrcjJ3rl3OJoEbkdD2kUnTKZSQa5D2uXhT9ChjDb5kbekthJoV95-2FPrDPeCtWyST67KSzzHFU-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>_______________________________________________<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><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div>+44 7523 279 698</div></div></div></div></div></div>
</div>