<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><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>&nbsp;</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>&nbsp;</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>&nbsp;</div>
<div>This would be implemented using some .gyb code that generates declarations that look something like<br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">func == &lt;A: Equatable, B: Equatable, C: Equatable&gt;(lhs: (A, B, C), rhs: (A, B, C)) -&gt; Bool {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; return lhs.0 == rhs.0 &amp;&amp; lhs.1 == rhs.1 &amp;&amp; lhs.2 == rhs.2<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}<br></span></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">func &lt; &lt;A: Comparable, B: Comparable, C: Comparable&gt;(lhs: (A, B, C), rhs: (A, B, C)) -&gt; Bool {<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; if lhs.0 != rhs.0 { return lhs.0 &lt; rhs.0 }<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; if lhs.1 != rhs.1 { return lhs.1 &lt; rhs.1 }<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; return lhs.2 &lt; rhs.2<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><br></div>
<div>&nbsp;</div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">func &gt; &lt;A: Comparable, B: Comparable, C: Comparable&gt;(lhs: (A, B, C), rhs: (A, B, C)) -&gt; Bool {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; if lhs.0 != rhs.0 { return lhs.0 &gt; rhs.0 }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; if lhs.1 != rhs.1 { return lhs.1 &gt; rhs.1 }</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; return lhs.2 &gt; rhs.2</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><br></div>
<div>&nbsp;</div>
<div>If tuples ever gain the ability to conform to protocols (which I think they should), and when types gain the ability to conditionally conform to protocols, these declarations would be adjusted to declare the tuples as conforming to Equatable and Comparable. This would be a backwards-compatible change.<br></div>
<div>&nbsp;</div>
<div>Similarly if Swift ever gains some form of variadic type parameters, thus allowing for declaring behavior of arbitrary-sized tuples, then this can be adjusted to use that feature and it should still be backwards-compatible.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
</body>
</html>