<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Dec 7, 2015 at 11:48 AM, Kevin Ballard 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"><u></u>
<div><div>I'd really like to see tuples support comparison operators when possible. Eventually they should even conform to the Equatable/Comparable protocols, but that requires tuples being able to conform to protocols at all to begin with.<br></div>
<div> </div>
<div>In the absence of some sort of variadic type parameters, we'd only be able to support the comparison operators on tuples up to some predefined arity. There's precedent in Rust and Haskell for this. Rust defines these operations up to arity 12, Haskell defines them up to arity 15.<br></div>
<div> </div>
<div>Behavior of == should be obvious. Behavior of the ordered comparison operators would work like comparison of strings, where the first element is compared, and if equal, the second element is compared, etc.<br></div>
<div> </div>
<div>This would be implemented using some .gyb code that generates declarations that look something like<br></div>
<div> </div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">func == <A: Equatable, B: Equatable, C: Equatable>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool {<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> return lhs.0 == rhs.0 && lhs.1 == rhs.1 && lhs.2 == rhs.2<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">}<br></span></div>
<div> </div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">func < <A: Comparable, B: Comparable, C: Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool {<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> if lhs.0 != rhs.0 { return lhs.0 < rhs.0 }<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> if lhs.1 != rhs.1 { return lhs.1 < rhs.1 }<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> return lhs.2 < rhs.2<br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">}</span><br></div>
<div> </div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">func > <A: Comparable, B: Comparable, C: Comparable>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool {</span><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> if lhs.0 != rhs.0 { return lhs.0 > rhs.0 }</span><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> if lhs.1 != rhs.1 { return lhs.1 > rhs.1 }</span><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"> return lhs.2 > rhs.2</span><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,"courier new",monospace,sans-serif">}</span></div></div></blockquote><div><br></div><div>Looks like a good idea to me! Also the <= and >= operators, right?</div><div><br></div><div>What concerns we with massive code generation is the code size of the core swift library. It would be definitely important to see how much code this adds, depending on the number of tuple elements.</div><div><br></div><div>I personally don't see a point in going as high as 12 tuple elements. About 4 or 5 makes sense to me. Given that Swift does not have variadic generics right now, these long tuples have to be defined by someone manually. If one is defining a tuple that is that long, I'd argue that they should be using a custom struct instead.</div><div><br></div><div>Dmitri</div></div><div><br></div>-- <br><div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</div></div>