<div dir="ltr">> would it be sensible to make these UInt?<div><br></div><div>It's really nice to have one Int type to rule over all integers. This means that a reasonable program can be written without casting.<br><div class="gmail_extra"><br></div><div class="gmail_extra">> Which would be unnecessary if using the type system with UInt.</div><div class="gmail_extra"><div><br></div><div>No, if subsript required UInt everyone would simply insert a cast, and -1 would still create problems as UInt.max.</div><div><br></div><div class="gmail_quote">On Thu, Dec 17, 2015 at 7:55 PM, Michael Armstrong via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi,<br>
<br>
My first post to this list so apologies if its not in the correct format or irrelevant / wrong :) Its quite simple, so i'm under the impression that perhaps there is already a very good reason why the below behaviour is on purpose, if this is the case i'd love to learn why, so view it as a proposal / question.<br>
<br>
When accessing Array's in Swift (and potentially other types) a Int (signed) is expected, as Array can't have a negative bounds in Swift (as far as i'm aware) would it be sensible to make these UInt? therefor removing the need for tests etc and guarding around negative integers?<br>
<br>
For Example:<br>
<br>
let someArray = ["a", "b", "c"]<br>
print(someArray.count.dynamicType) // Int<br>
<br>
let someIndex = 1<br>
print(someArray[someIndex]) // b<br>
<br>
let anotherIndex : UInt = 1<br>
print(someArray[anotherIndex]) // cannot subscript a value of type '[String]' with an index of type 'UInt'<br>
<br>
let invalidIndex = -1<br>
print(someArray[invalidIndex]) // Darwin: EXC_BAD_INSTRUCTION / Linux REPL: Error running code: unknown error code 132.<br>
<br>
print (invalidIndex.dynamicType) // Int<br>
<br>
If Array took a UInt, the line 'print(someArray[invalidIndex])' would fail at COMPILE time rather than RUNTIME.<br>
<br>
<br>
Looking at the Swift source there are things like<br>
<br>
_sanityCheck(count >= 0)<br>
<br>
Which would be unnecessary if using the type system with UInt.<br>
<br>
I'm aware that almost everyone bounds checks Array's before access etc, however is there a reason someone can explain for not using a 'UInt' in these circumstances for 'Array' and other collection types.<br>
<br>
Thanks,<br>
<br>
Michael<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><br></div></div></div>