UInt64 indices broken


(G Alexander) #1

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?


(John McCall) #2

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.

···

On Mar 15, 2017, at 9:04 PM, G Alexander via swift-evolution <swift-evolution@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?


(Xiaodi Wu) #3

This is a deliberate design choice. As written in The Swift Programming
Language:

"Use UInt only when you specifically need an unsigned integer type with the
same size as the platform’s native word size. If this is not the case, Int
is preferred, even when the values to be stored are known to be
non-negative. A consistent use of Int for integer values aids code
interoperability, avoids the need to convert between different number
types, and matches integer type inference, as described in Type Safety and
Type Inference."

You are correct that arrays with count greater than Int.max are unsupported.

···

On Thu, Mar 16, 2017 at 11:13 G Alexander via swift-evolution < swift-evolution@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?

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution