[swift-evolution] UInt64 indices broken

John McCall rjmccall at apple.com
Thu Mar 16 12:15:57 CDT 2017


> On Mar 15, 2017, at 9:04 PM, G Alexander via swift-evolution <swift-evolution at swift.org> wrote:
> 
> All,
> 
> Why does swift fails to fully implement UInt in any object, arrays, sequence, range, etc
> 
> Initializer for Range, from "http://swiftdoc.org/v3.1/type/Range/"
> 
>  For example, passing a closed range with an upper bound of Int.max triggers a runtime error, because the resulting half-open range would require an upper bound of Int.max + 1, which is not representable as an Int.
> 
> So any algorithm that needs large arrays such that,   UInt64.max > index > Int64  too bad?

You keep slipping between different types, so it's hard to figure out what you're asking about.

Ranges can be expressed using an arbitrary bound type, and Collections can be expressed using an arbitrary index type.  So if you want to have a collection whose index type is UInt64, you absolutely can.  The standard arrays use Int as their index type because (1) there's value in promoting a single integer type and (2) those types store their elements in memory, and the machine almost certainly cannot store anywhere near Int.max values in memory at once, even if they're a single byte apiece.

Closed ranges are implemented in terms of half-open ranges because trying to support both faithfully would massively complicate the programming model around ranges.  Losing a single element of capacity from integer-indexed collections is comparatively innocuous.

John.


More information about the swift-evolution mailing list