<!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> </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 class="font" 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 class="font" 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 class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}<br></span></div>
<div> </div>
<div><span class="font" 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 class="font" 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 class="font" 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 class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> return lhs.2 < rhs.2<br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span><br></div>
<div> </div>
<div><span class="font" 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 class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> if lhs.0 != rhs.0 { return lhs.0 > rhs.0 }</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> if lhs.1 != rhs.1 { return lhs.1 > rhs.1 }</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> return lhs.2 > rhs.2</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br></span></div>
<div><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span><br></div>
<div> </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> </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> </div>
<div>-Kevin Ballard</div>
</body>
</html>