[swift-users] Range<UInt64>.count can abort
Dmitri Gribenko
gribozavr at gmail.com
Fri Apr 15 18:26:41 CDT 2016
On Fri, Apr 15, 2016 at 4:23 PM, Jens Alfke via swift-users
<swift-users at swift.org> wrote:
> 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.
>
> The trouble is, if I represent these with Ranges whose endIndex is
> UInt64.max, those ranges tend to bomb:
>
> let r: Range<UInt64> = 5..<UInt64.max
> r.count // FATAL ERROR
>
>
> 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?
Hi Jens,
The distance is signed so that we can represent negative distances.
r.distance(from:to:) should be able to measure distances backwards.
We are aware of this issue, but we don't know of a good solution.
We'd appreciate your suggestions.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-users
mailing list