Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the rationale of Swift's size methods taking `Int`s?

Tags:

swift

I've noticed a lot of swift built ins take or return Ints and not UInts:

Here are some examples from Array:

mutating func reserveCapacity(minimumCapacity: Int)

var capacity: Int { get }

init(count: Int, repeatedValue: T)

mutating func removeAtIndex(index: Int) -> T

Given that the language is completely new, and assuming that this design choice was not arbitrary - I'm wondering: Why do swift built ins take Ints and not UInts?

Some notes: Asking because I'm working on a few collections myself and I'm wondering what types I should use for things like reserveCapacity etc. What I'd naturally expect is for reserveCapacity to take a UInt instead.

like image 709
Benjamin Gruenbaum Avatar asked Sep 22 '14 11:09

Benjamin Gruenbaum


2 Answers

UInt is a common cause of bugs. It is very easy to accidentally generate a -1 and wind up with infinite loops or similar problems. (Many C and C++ programmers have learned the hard way that you really should just use int unless there's a need for unsigned.) Because of how Swift manages type conversion, this is even more important. If you've ever worked with NSUInteger with "signed-to-unsigned" warnings turned on (which are an error in Swift, not an optional warning like in C), you know what a pain that is.

The Swift Programming Guide explains their specific rationale in the section on UInt:

NOTE

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.

like image 134
Rob Napier Avatar answered Oct 21 '22 15:10

Rob Napier


Here is a possible explanation (I am no expert on this subject): Suppose you have this code

let x = 3
test(x)

func test(t: Int) {
}

This will compile without a problem, since the type of 'x' is inferred to be Int.

However, if you change the function to

func test(t: UInt) {
}

The compiler will give you a build error ('Int' is not convertible to 'UInt')

So my guess is that it is just for convenience, because Swift's type safety would otherwise require you to convert them manually each time.

like image 38
Atomix Avatar answered Oct 21 '22 16:10

Atomix