It might be worth noting that Array's existing subscript behavior is not that unique after all, if looking at it like this:
let arr: Array<Int> = [1, 2, 3]
let _ = arr[arr.endIndex] // Fatal error
let str: String = "abc"
let _ = str[str.endIndex] // Fatal error
let set: Set<String> = ["a", "bc", "def"]
let _ = set[set.endIndex] // Fatal error
let dic: Dictionary<String, Int> = ["a" : 1, "b": 2]
let _ = dic[dic.endIndex] // Fatal error
What if, instead of adding another subscript for Array
's Index
, we add a subscript for some new type (FooIndex), similar to how Set
and Dictionary
have subscripts for not only their Index
(trapping if it's out of bounds, as seen above), but also for their Element
/Key
(returning an Optional)?