[swift-evolution] Arrays Returning Optionals instead of Index Out of Bounds

Andreas Ley andreas at ley.info
Thu Jun 23 05:12:23 CDT 2016

(First time using a mailing list; I hope this message ends up in the correct thread)

This is a topic that comes up regularly on the Swift evolution mailing list and off it.
After reading through all the respective threads again, there seem to be the following two camps:

Arguments made for crashing when accessing a non-existent index: 
- fast
- shows bugs quickly 

Arguments made in favor of returning an optional by default: 
- safe (as in "doesn't crash")
- similar to what other modern languages do
- what an unexperienced Swift developer would expect

All are valid arguments, but for different use cases. 
In my opinion, the biggest problem is that there's no indication that subscripting can crash on the default array. Alternative subscripts for bounded access wouldn't solve this either.

Maybe Swift should have two different array classes: A fast, fast-failing "UnsafeArray" and a default safe "Array". This would prevent unexpected crashes for new Swift programmers while still providing a faster alternative for those who do low-level stuff. The name "UnsafeArray" would clearly communicate that this class should be handled with care.

- Andreas

More information about the swift-evolution mailing list