Why does the Swift language guide suggest using Int "even when values are known to be non-negative"?
I don't think using UInt is as safe as you think it is. As you noted:
let age:UInt = -3
results in a compiler error. I also tried:
let myAge:Int = 1
let age:UInt = UInt(myAge) - 3
which also resulted in a compiler error. However the following (in my opinion much more common in real programs) scenarios had no compiler error, but actually resulted in runtime errors of EXC_BAD_INSTRUCTION
:
func sub10(num: Int) -> UInt {
return UInt(num - 10) //Runtime error when num < 10
}
sub10(4)
as well as:
class A {
var aboveZero:UInt
init() { aboveZero = 1 }
}
let a = A()
a.aboveZero = a.aboveZero - 10 //Runtime error
Had these been plain Int
s, instead of crashing, you could add code to check your conditions:
if a.aboveZero > 0 {
//Do your thing
} else {
//Handle bad data
}
I might even go so far as to equate their advice against using UInt
s to their advice against using implicitly unwrapped optionals: Don't do it unless you are certain you won't get any negatives, because otherwise you'll get runtime errors (except in the simplest of cases).
It says in your question.. "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."
This avoids issues such as assigning an Int to an UInt. Negative Int values assigned to UInts result in large values instead of the intended negative value. The binary representation of both doesn't differentiate one type from the other.
Also, both are classes, one not descended from the other. Classes built receive Ints cannot receive UInts without overloading, meaning converting between the two would be a common task of UInts are being used when most of the framework receives Ints. Converting between the two can become a non-trival task as well.
The two previous paragraphs speak to "interoperability" and "converting between different number types". Issues that are avoided if UInts aren't used.