<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Is there any reason that Distance has to be a simple Int? Since it’s defined per-type, UInt64 could use a custom struct without impacting the performance of other types:<div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> UInt64Distance : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Comparable</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures" class="">//I'm not sure what else it needs to conform to </span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> distance: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt64</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> isPositive: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == (lhs: </span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64Distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, rhs: </span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64Distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> lhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == rhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> && lhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">isPositive</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> == rhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">isPositive</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> < (lhs: </span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64Distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, rhs: </span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64Distance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (lhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">isPositive</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, rhs.</span><span style="font-variant-ligatures: no-common-ligatures" class="">isPositive</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">false</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">false</span><span style="font-variant-ligatures: no-common-ligatures" class="">): </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">distance</span><span style="font-variant-ligatures: no-common-ligatures" class=""> > rhs.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">distance</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">case</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures" class="">false</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">true</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> ): </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">true</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">case</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures" class="">true</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">false</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">): </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">false</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">true</span><span style="font-variant-ligatures: no-common-ligatures" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">true</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ): </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">distance</span><span style="font-variant-ligatures: no-common-ligatures" class=""> < rhs.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">distance</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">...</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(0, 132, 0); font-variant-ligatures: no-common-ligatures;" class="">//The rest of `Comparable`</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class="">(I’m assuming it’d need `Comparable`, but a quick glance around with cmd-click in Xcode didn’t tell me what, if any, protocols `Distance` needs to conform to.)</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">On Apr 15, 2016, at 6:26 PM, Dmitri Gribenko via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">On Fri, Apr 15, 2016 at 4:23 PM, Jens Alfke via swift-users<br class=""><<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I’m writing some code that deals with ranges of “sequence numbers”, which<br class="">are consecutively-assigned positive 64-bit integers, thus Range<UInt64> in<br class="">Swift. I’m having trouble handling half-open ranges with no maximum, which<br class="">are very commonly used for representing all items created since a certain<br class="">time.<br class=""><br class="">The trouble is, if I represent these with Ranges whose endIndex is<br class="">UInt64.max, those ranges tend to bomb:<br class=""><br class="">let r: Range<UInt64> = 5..<UInt64.max<br class="">r.count // FATAL ERROR<br class=""><br class=""><br class="">The problem is that Range.count’s type is Element.Distance, and<br class="">UInt64.Distance is a typealias of … Int. Huh? It’s pretty clear that the<br class="">distance between two UInt64s can’t be represented by a signed Int64. Why<br class="">isn’t Distance UInt64?<br class=""></blockquote><br class="">Hi Jens,<br class=""><br class="">The distance is signed so that we can represent negative distances.<br class="">r.distance(from:to:) should be able to measure distances backwards.<br class=""><br class="">We are aware of this issue, but we don't know of a good solution.<br class="">We'd appreciate your suggestions.<br class=""><br class="">Dmitri<br class=""><br class="">-- <br class="">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br class="">(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/<br class="">_______________________________________________<br class="">swift-users mailing list<br class="">swift-users@swift.org<br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></blockquote><br class=""></div></div></body></html>