If I bind an index computed from one collection type, such as a String
, it seems I can reuse that index for other collections. For example:
var str1 = "Hello, World!"
var dex1 = str1.startIndex
var str2 = "Goodbye, All!"
str2[dex1] => "G"
This makes some sense if one thinks of this String.Index
as an integer offset (into an array); however, an Index as an Integer is far from a requirement. Yet, many/all builtin Swift collection types: Array
, Dictionary
, Set
and String
all have these 'reusable' indexes.
It this 'reuse' across collection types a requirement? I've not noted it in any documentation; have I overlooked it?
In the above code example, I had expected:
> str2[dex1]
Exception: dex1 is not an index of str2
[String is just an example; see the question in bold]