<div dir="ltr">Can't you define this yourself? This worked in a Playground using Swift 2.1:<div><br></div>enum Ordering {<br> case OrderedSame, OrderedAscending, OrderedDescending<br>}<br><br>infix operator <=> {}<br>func <=> <T: Comparable>(x: T, y: T) -> Ordering {<br> if x < y {<br> return .OrderedAscending<br> } else if x > y {<br> return .OrderedDescending<br> } else {<br> return .OrderedSame<br> }<br>}<br><br><br>let x = 3<br>let y = 4<br>switch x <=> y {<br>case .OrderedSame:<br> print(3)<br>case .OrderedAscending:<br> print(4) // Executed<br>case .OrderedDescending:<br> print(5)<br>}</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 4, 2016 at 12:52 PM, Ross O'Brien 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"><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 'if { } else if { } else { }' structure. A switch can be used (case _ where x < y) but the compiler will complain that the switch is not exhaustive, resulting in a redundant 'default' case.</div><div><br></div>I'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)"> <=> {}</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)"> <=> <C : </span>Comparable<span style="color:rgb(0,0,0)">> (lhs: </span>C<span style="color:rgb(0,0,0)">, rhs:</span>C<span style="color:rgb(0,0,0)">) -> </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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">                </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 style="white-space:pre-wrap">        </span>}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">                </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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">        </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)"><=></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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">        </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 style="white-space:pre-wrap">        </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><span class="HOEnZb"><font color="#888888"><div><br></div><div>-- Ross</div></font></span></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>