<div dir="ltr"><div>Right... per &quot;[swift-evolution] [Idea] Find alternatives to `switch self`&quot;<br></div><div> it was only &quot;impossible&quot; in my head.<div><br></div><div><div>Thanks Dany </div></div><div><br></div><div>This proposal is unnecessary.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 23, 2016 at 8:53 PM, <a href="mailto:charles@charlesism.com">charles@charlesism.com</a> <span dir="ltr">&lt;<a href="mailto:charlesism.com@gmail.com" target="_blank">charlesism.com@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>I&#39;m not actually familiar with the term &quot;tri op&quot; but if you&#39;re referring to the ternary, it&#39;s only useful when you two, or three items. </div><div><br></div><div>If you chain a ternary to use more than three options it becomes error-prone and almost impossible for a human to read<br><br>When I&#39;m at my desktop I&#39;ll add a couple better examples of what I&#39;m proposing. <br><br>Sent from my iPhone</div><div><div class="h5"><div><br>On May 23, 2016, at 6:18 PM, Dany St-Amant &lt;<a href="mailto:dsa.mls@icloud.com" target="_blank">dsa.mls@icloud.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div><br></div>Why reinvent the wheel, when the old trusty (but a bit cryptic according to some) tri-op can do the trick…<div><br><div><blockquote type="cite"><div>Le 23 mai 2016 à 04:29, Charles Constant via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; a écrit :</div><br><div><div dir="ltr">Here&#39;s a few examples of what this change would allow. <div><br></div><div>I just plucked the first instances of other people&#39;s switch statements that I found on GitHub. </div><div><br></div><div>If there were an easy way to search GitHub for chained ternary expressions, I would have added some examples of those too, since they could all be improved with this where clause + ??. <div><br></div><div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">        </span></span><span style="color:rgb(194,52,155)">mutating</span><span> </span><span style="color:rgb(194,52,155)">func</span><span> toggle() {</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                </span></span><span>switch</span><span style="color:rgb(255,255,255)"> </span><span>self</span><span style="color:rgb(255,255,255)">{</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span></span><span style="color:rgb(194,52,155)">case</span><span> Off:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                        </span></span><span style="color:rgb(194,52,155)">self</span><span> = On</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span></span><span style="color:rgb(194,52,155)">case</span><span> On:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                        </span></span><span style="color:rgb(194,52,155)">self</span><span> = Off<span style="white-space:pre-wrap">        </span></span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>}<span style="white-space:pre-wrap">        </span></span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">        </span>}</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">        </span></span><span style="color:rgb(194,52,155)">mutating</span><span> </span><span style="color:rgb(194,52,155)">func</span><span> toggle() {</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">                </span></span><span style="color:rgb(194,52,155)">self</span><span> = .On </span><span style="color:rgb(194,52,155)">where</span><span> (</span><span style="color:rgb(194,52,155)">self</span><span> == .Off) ?? .Off</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">        </span>}</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><br><span></span></div></div></div></div></div></blockquote><div><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">mutating</span> <span style="color:#bb2ca2">func</span> toggle() { <span style="color:#bb2ca2">self</span> = <span style="color:#bb2ca2">self</span> <span style="color:#31595d">==</span> .<span style="color:#31595d">Off</span> ? .<span style="color:#31595d">On</span> : .<span style="color:#31595d">Off</span> }</div><div><br></div><blockquote type="cite"><div dir="ltr"><div><div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">        </span></span><span style="color:rgb(194,52,155)">switch</span><span> switchNumberThree {</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span></span><span style="color:rgb(194,52,155)">case</span><span> </span><span style="color:rgb(139,132,207)">10</span><span>, </span><span style="color:rgb(139,132,207)">11</span><span>, </span><span style="color:rgb(139,132,207)">12</span><span>:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                        </span>println(</span><span style="color:rgb(228,68,72)">&quot;It is </span><span>\</span><span style="color:rgb(228,68,72)">(</span><span>switchNumberThree</span><span style="color:rgb(228,68,72)">)&quot;</span><span>)</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                </span></span><span>default</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(228,68,72);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                        </span>(</span><span>&quot;It is none of them!&quot;</span><span style="color:rgb(255,255,255)">)</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">        </span>}</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">        </span>println(</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">                </span></span><span style="color:rgb(228,68,72)">&quot;It is </span><span>\</span><span style="color:rgb(228,68,72)">(</span><span>switchNumberThree</span><span style="color:rgb(228,68,72)">)&quot;</span><span> </span><span style="color:rgb(194,52,155)">where</span><span> </span><span style="color:rgb(139,132,207)">10</span><span>...</span><span style="color:rgb(139,132,207)">12</span><span> ~= switchNumberThree</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(228,68,72);background-color:rgb(0,30,87)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                </span>?? </span><span>&quot;It is none of them!&quot;</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">        </span>)</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><br><span></span></div></div></div></div></blockquote><br><div><div style="margin:0px;line-height:normal"><span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px">print</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">( </span><span style="color:rgb(39,42,216);font-family:Menlo;font-size:11px">10</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">...</span><span style="color:rgb(39,42,216);font-family:Menlo;font-size:11px">12</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px"> ~= </span><font color="#4f8187" face="Menlo"><span style="font-size:11px">switchNumberThree</span></font><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px"> ? </span><span style="color:rgb(209,47,27);font-family:Menlo;font-size:11px">&quot;It is </span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">\</span><span style="color:rgb(209,47,27);font-family:Menlo;font-size:11px">(</span><font color="#4f8187" face="Menlo"><span style="font-size:11px">switchNumberThree</span></font><span style="color:rgb(209,47,27);font-family:Menlo;font-size:11px">)</span><font color="#d12f1b" face="Menlo"><span style="font-size:11px">&quot;</span></font></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#000000">       : </span><span style="color:#d12f1b">&quot;It&#39;s none of them&quot;</span><span style="color:#000000"> )</span></div><div><br></div></div><blockquote type="cite"><div dir="ltr"><div><div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>switch</span><span style="color:rgb(255,255,255)"> x {</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>case</span><span style="color:rgb(255,255,255)"> </span><span style="color:rgb(139,132,207)">1</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>case</span><span style="color:rgb(255,255,255)"> </span><span style="color:rgb(139,132,207)">2</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>case</span><span style="color:rgb(255,255,255)"> </span><span style="color:rgb(139,132,207)">3</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>case</span><span style="color:rgb(255,255,255)"> </span><span style="color:rgb(139,132,207)">4</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                </span></span><span>fallthrough</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>case</span><span style="color:rgb(255,255,255)"> </span><span style="color:rgb(139,132,207)">5</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">                </span></span><span>fallthrough</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(194,52,155);background-color:rgb(92,7,0)"><span style="color:rgb(255,255,255)"><span style="white-space:pre-wrap">        </span></span><span>default</span><span style="color:rgb(255,255,255)">:</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">                </span>j++</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0)"><span><span style="white-space:pre-wrap">        </span>}</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(92,7,0);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><span></span><br></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87)"><span><span style="white-space:pre-wrap">        </span>j = j+</span><span style="color:rgb(139,132,207)">1</span><span> </span><span style="color:rgb(194,52,155)">where</span><span> (</span><span style="color:rgb(139,132,207)">4</span><span>...</span><span style="color:rgb(139,132,207)">5</span><span> ~= x) ?? j+</span><span style="color:rgb(139,132,207)">2</span></div><div style="margin:0px;font-size:10px;line-height:normal;font-family:&#39;Fantasque Sans Mono&#39;;color:rgb(255,255,255);background-color:rgb(0,30,87);min-height:12px"><br><span></span></div></div></div></div></blockquote><div><br></div><div>Broken conversion:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#4f8187">j</span> += <span style="color:#272ad8">4</span>...<span style="color:#272ad8">5</span> ~= <span style="color:#4f8187">x</span> ? <span style="color:#272ad8">1</span> : <span style="color:#272ad8">2</span></div><div><span style="color:#272ad8"><br></span></div></div><div>Proper conversion:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#4f8187">j</span> += <span style="color:#272ad8">4</span> ~= <span style="color:#4f8187">x</span> ? <span style="color:#272ad8">3</span> : <span style="color:#272ad8">5</span> ~= <span style="color:#4f8187">x</span> ? <span style="color:#272ad8">2</span> : <span style="color:#272ad8">1</span></div><div><span style="color:#272ad8"><br></span></div></div>Earlier e-mail example:</div><div><blockquote type="cite"><div style="font-size:13px"><b>    let foo = </b></div><div style="font-size:13px"><b>        &quot;positive&quot; where ( bar &gt; 0 )  ?? </b></div><div style="font-size:13px"><b>        &quot;negative&quot; where ( bar &lt; 0 ) ?? </b></div><div style="font-size:13px"><b>        &quot;zero&quot;</b></div></blockquote><div><div style="font-size:13px"><b><br></b></div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">let</span> foo = <span style="color:#4f8187">bar</span> &gt; <span style="color:#272ad8">0</span> ? <span style="color:#d12f1b">&quot;positive&quot;</span> :</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">          <span style="color:#4f8187">bar</span> &lt; <span style="color:#272ad8">0</span> ? <span style="color:#d12f1b">&quot;negative&quot;</span> :</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">          <span style="color:#d12f1b">&quot;zero&quot;</span></div><div><span style="color:#d12f1b"><br></span></div></div><div>Dany</div></div></div></blockquote></div></div></div></blockquote></div><br></div>