<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
I’m writing some code that deals with ranges of “sequence numbers”, which are consecutively-assigned positive 64-bit integers, thus Range<UInt64> in Swift. I’m having trouble handling half-open ranges with no maximum, which are very commonly used for representing
all items created since a certain time.
<div class=""><br class="">
</div>
<div class="">The trouble is, if I represent these with Ranges whose endIndex is UInt64.max, those ranges tend to bomb:</div>
<div class=""><br class="">
</div>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">
<div class="">
<div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #005493" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> r:
</span><span style="font-variant-ligatures: no-common-ligatures" class="">Range</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>
= </span><span style="font-variant-ligatures: no-common-ligatures; color: #424242" class="">5</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures" class="">UInt64</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.max</span></div>
</div>
<div class="">
<div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Consolas; color: rgb(112, 61, 170);" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #80257f" class="">r</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">count
// FATAL ERROR</span></div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">The problem is that Range.count’s type is Element.Distance, and UInt64.Distance is a typealias of … Int. Huh? It’s pretty clear that the distance between two UInt64s can’t be represented by a signed Int64. Why isn’t Distance UInt64?</div>
<div class=""><br class="">
</div>
<div class="">(I’m guessing it’s because Distance needs to be signed, to represent backwards distances? But that’s not needed for Ranges, of course.)</div>
<div class=""><br class="">
</div>
<div class="">It’s sort of worrisome that Swift will let me create a valid Range value that nonetheless bombs when accessed! I think I’m going to stop using Range for working with sequence numbers :/</div>
<div class=""><br class="">
</div>
<div class="">—Jens</div>
</body>
</html>