[swift-evolution] [Review] SE-0065 A New Model for Collections and Indices

Dmitri Gribenko gribozavr at gmail.com
Mon May 2 20:19:27 CDT 2016


On Mon, May 2, 2016 at 3:08 PM, Karl via swift-evolution
<swift-evolution at swift.org> wrote:
> So, just confirming that the following is going to change:
>
> —————
> var string    = "test"
> let origLen   = 4
> let indexOne  = string.startIndex
>
> string.appendContentsOf("ANOTHERTEST")
>
> let indexTwo = indexOne.advancedBy(origLen, limit: string.endIndex)
>
> assert(indexTwo<string.endIndex)
> indexTwo.successor()                           //<--- fatal error: cannot increment endIndex
> —————

Hi Karl,

Yes, this code would start working.

> Is this a consequence of the index needing to know its originating string for successor() purposes?

That would be a plausible explanation, but I think that would be too
simple to be true :)  This code should even work in the current model,
but the current (buggy) behavior is caused by String indices keeping
an extra strong reference to the underlying storage, so the mutation
actually detaches the index from the updated string storage.

> Once this is implemented, will String.Index be portable across string instances (presuming you bounds-check them, as I did above)?

Yes, within the prefix that was common to all strings and was created
in a common mutation history.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list