<div dir="ltr"><div>Compare two values, and react differently if the first is larger, the second is larger, or they are equal.</div><div><br></div><div>An if statement will result in an &#39;if { } else if { } else { }&#39; structure. A switch can be used (case _ where x &lt; y) but the compiler will complain that the switch is not exhaustive, resulting in a redundant &#39;default&#39; case.</div><div><br></div>I&#39;d like to propose this addition to the Comparable type:<div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">infix<span style="color:rgb(0,0,0)"> </span>operator<span style="color:rgb(0,0,0)"> &lt;=&gt; {}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> &lt;=&gt; &lt;C : </span>Comparable<span style="color:rgb(0,0,0)">&gt; (lhs: </span>C<span style="color:rgb(0,0,0)">, rhs:</span>C<span style="color:rgb(0,0,0)">) -&gt; </span>NSComparisonResult</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span><span style="color:rgb(187,44,162)">if</span> lhs <span style="color:rgb(61,29,129)">==</span> rhs</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">                </span></span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> .</span>OrderedSame</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span><span style="color:rgb(187,44,162)">if</span> lhs <span style="color:rgb(61,29,129)">&lt;</span> rhs</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">                </span></span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> .</span>OrderedAscending</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span class="" style="white-space:pre">        </span>}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">        </span></span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> .</span>OrderedDescending</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:arial,sans-serif;font-size:small">This now allows code like this:</span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:arial,sans-serif;font-size:small"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> x = <span style="color:rgb(39,42,216)">3</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> y = <span style="color:rgb(39,42,216)">4</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">switch<span style="color:rgb(0,0,0)"> x </span><span style="color:rgb(49,89,93)">&lt;=&gt;</span><span style="color:rgb(0,0,0)"> y</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">{</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">case</span> .OrderedSame:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">        </span></span>print<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(39,42,216)">3</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">case</span> .OrderedAscending:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">        </span></span>print<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(39,42,216)">4</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">case</span> .OrderedDescending:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">        </span></span>print<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(39,42,216)">5</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">









</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><br></div><div>-- Ross</div></div>