<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">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</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&#39;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&#39;d only be able to support the comparison operators on tuples up to some predefined arity. There&#39;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,&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 style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    return lhs.0 == rhs.0 &amp;&amp; lhs.1 == rhs.1 &amp;&amp; lhs.2 == rhs.2<br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">}<br></span></div>
<div> </div>
<div><span 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 style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    if lhs.0 != rhs.0 { return lhs.0 &lt; rhs.0 }<br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    if lhs.1 != rhs.1 { return lhs.1 &lt; rhs.1 }<br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    return lhs.2 &lt; rhs.2<br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">}</span><br></div>
<div> </div>
<div><span 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 style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    if lhs.0 != rhs.0 { return lhs.0 &gt; rhs.0 }</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    if lhs.1 != rhs.1 { return lhs.1 &gt; rhs.1 }</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">    return lhs.2 &gt; rhs.2</span><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif"><br></span></div>
<div><span style="font-family:menlo,consolas,&quot;courier new&quot;,monospace,sans-serif">}</span></div></div></blockquote><div><br></div><div>Looks like a good idea to me!  Also the &lt;= and &gt;= 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&#39;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&#39;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&lt;i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;*/</div>
</div></div>