<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Does an empty Int sort before or after a non-empty Int?<br><br>Sent from my iPhone</div><div><br>On Aug 27, 2016, at 22:34, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div>


<title></title>

<div>On Sat, Aug 27, 2016, at 07:07 AM, Javier Soto via swift-evolution wrote:<br></div>
<blockquote type="cite"><div>My biggest issue with Optional conforming to Comparable is that while a default implementation may sometimes be useful, it may not necessarily be the one you want.<br></div>
</blockquote><div><br></div>
<div>Isn't that true for almost everything? I could use the same argument to say String shouldn't implement &lt;, because it's not always the comparison you want. For example, with the lastName example, you probably want case-insensitive comparisons instead of case-sensitive comparisons, so you probably shouldn't be using &lt; at all.<br></div>
<div><br></div>
<blockquote type="cite"><div>In that last example with lastName, if you wanted to change the policy for whether users without last name appear first or last, you'd have to write more verbose code anyway. Generally, reading that code with just "&lt;" I would never remember what Swift would do with nil (do they go first or last?)<br></div>
</blockquote><div><br></div>
<div>Why would nil sort to the end? Empty values typically sort before non-empty values, for example the empty string sorts before all other strings.<br></div>
<div><br></div>
<div>-Kevin</div>
<div><br></div>
<blockquote type="cite"><div>If you don't care that much, a simple one-liner without all those guards could also be:<br></div>
<div><br></div>
<div>users.sorted { ($0.lastName ?? "") &lt; ($1.lastName ?? "") }<br></div>
<div defang_data-gmailquote="yes"><div dir="ltr">On Sat, Aug 27, 2016 at 6:58 AM Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex;" defang_data-gmailquote="yes"><div style="word-wrap:break-word;"><div>I have personally (ab)used this for the following:<br></div>
<div><br></div>
<div>class User {<br></div>
<div><span style="white-space:pre-wrap;"></span>var email: String<br></div>
<div><span style="white-space:pre-wrap;"></span>var firstName: String?<br></div>
<div><span style="white-space:pre-wrap;"></span>var lastName: String?<br></div>
<div>}<br></div>
<div><br></div>
<div>You have a list of users based on email, so last name is optional. In Swift 2.x, you can do:<br></div>
<div><br></div>
<div>users.sort({ $0.lastName &lt; $1.lastName })<br></div>
<div><br></div>
<div>Now, you need to do:<br></div>
<div><br></div>
<div><div>users.sorted({<br></div>
<div><span style="white-space:pre-wrap;"> </span>guard&nbsp;let&nbsp;firstName = $0.0.lastName&nbsp;else&nbsp;{<br></div>
<div><span style="white-space:pre-wrap;"> </span>return&nbsp;true<br></div>
<div><span style="white-space:pre-wrap;"> </span>}<br></div>
<div><span style="white-space:pre-wrap;"> </span><br></div>
<div><span style="white-space:pre-wrap;"> </span>guard&nbsp;let&nbsp;secondName = $0.1.lastName&nbsp;else&nbsp;{<br></div>
<div><span style="white-space:pre-wrap;"> </span>return&nbsp;false<br></div>
<div><span style="white-space:pre-wrap;"> </span>}<br></div>
<div><span style="white-space:pre-wrap;"> </span><br></div>
<div><span style="white-space:pre-wrap;"> </span>return&nbsp;firstName &lt; secondName<br></div>
<div>})<br></div>
</div>
<div><br></div>
<div>Which aside from being a brain teaser how to properly maintain ordering when $0.0's lastName != nil &amp;&amp; $0.1's lastName == nil, adds additional few lines.<br></div>
<div><br></div>
<div>But I agree that it may come as confusing with Ints, etc. - with strings it kind of makes sense since nil is like an empty string which is placed in front of everything.<br></div>
</div>
<div style="word-wrap:break-word;"><div><div><br></div>
<div><blockquote type="cite"><div>On Aug 27, 2016, at 1:46 PM, Haravikk via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div><br></div>
<div><div style="word-wrap:break-word;"><div><br></div>
<div><blockquote type="cite"><div>On 27 Aug 2016, at 02:01, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
<div><div><div><br></div>
<div>This change is going to have a HUGE impact for me. I use this sort of comparison _all the time_ and find it incredibly useful, and have had literally zero bugs caused by this. Surely I can't be the only one who uses this. I am not looking forward to copying &amp; pasting a reimplementation of the comparison functions into every single project I work on.<br></div>
</div>
</div>
</blockquote><div><br></div>
</div>
<div>Can you give some examples as to how this will have such a huge impact? Now that we have the ?? operator it seems that this is fairly easy to replace:<br></div>
<div><br></div>
<div><span class="font" style="font-family:Monaco"><span style="white-space:pre-wrap;"></span>value &lt; 5 // where value is of type Int?</span><br></div>
<div><br></div>
<div>With:<br></div>
<div><br></div>
<div><span class="font" style="font-family:Monaco"><span style="white-space:pre-wrap;"></span>(value ?? 0) &lt; 5</span><br></div>
<div><span class="font" style="font-family:Monaco"></span><br></div>
<div><br></div>
<div>The latter is completely clear what the behaviour of nil is.<br></div>
<div><br></div>
<div>Also, you can still re-add the operators where you need them, ideally with as limited a type as possible so you can make sure that it's behaviour is well defined.<br></div>
</div>
<div>_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</div>
</blockquote></div>
<div><br></div>
</div>
</div>
<div>_______________________________________________<br></div>
<div> swift-evolution mailing list<br></div>
<div> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote></div>
<div dir="ltr">-- <br></div>
<div><div dir="ltr">Javier Soto<br></div>
</div>
<div><u>_______________________________________________</u><br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote><div><br></div>


</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>