-1

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]

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • What's the question? There's nothing "reusable" going on here. The whole concept "If I bind an index computed from one instance" is just fluff. This is just a type like any other type. `str1.startIndex` is, as you rightly say, a `String.Index`. Well, so you can use it wherever a `String.Index` is usable. And subscripting a String is one of the places it is usable. And so on. It isn't "bound" to anything. – matt Apr 15 '15 at 03:20
  • This is a general question about requirements on indices. If I create a Tree type with parent/child nodes and I base the indexing on walking the nodes... then the root node/index in one tree *won't* be useable in another tree. – GoZoner Apr 15 '15 at 03:31
  • Why won't it be usable? A type is a type. Your "not an index of str2" is just something you're making up for no reason. It might be out of range but it is still a value of the right type. – matt Apr 15 '15 at 04:01
  • Ranges (and indices) created for one string cannot be reliably used for a different string, see http://stackoverflow.com/a/24056932/1187415. – Martin R Apr 15 '15 at 04:51
  • @MartinR I didn't say it was reliable. I said it was usable, i.e. syntactically legal. Naturally this will crash: `"x"[advance("hello".startIndex,1)]` But it isn't illegal and there's nothing surprising about that. It is a silly thing to do, but there is nothing about the nature of `String.Index` that makes it surprising that you can do it. – matt Apr 15 '15 at 06:06
  • @matt: My comment was more directed to OP than to you. And the example that I had in mind (from the referenced thread) is `let x = ""[advance("hello".startIndex,1)]` which produces garbage (because advancing by 1 in "hello" does something different than advancing by 1 in ""). – Martin R Apr 15 '15 at 07:22

1 Answers1

0

The answer is, from the Apple forums, summarizing: The observed behavior:

  • is not a requirement
  • should not be relied upon
  • for value types, it may be difficult to throw an exception
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • I've noticed that they have figured out to throw an exception in the one case where you use `endIndex` as a subscript or try to advance positively beyond it, so clearly it is marked in a special way. But otherwise this definitely accords with what we were getting at in the comments, namely, this is not really surprising but don't do it. – matt Apr 15 '15 at 16:10