[swift-evolution] Strings in Swift 4

Karl Wagner razielim at gmail.com
Sat Feb 11 07:16:46 CST 2017


> On 11 Feb 2017, at 04:23, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> On Feb 10, 2017, at 5:49 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> An easier to implement, but slightly less useful approach, would be to have methods which take an array of indexes along with the proposed change, and then it adjusts the indexes (or replaces them with nil if they are invalid) as it makes the update.  For example:
>> 
>> 	func append(_ element:Element, adjusting: [Index]) -> [Index?]
>> 	func appending(_ element:Element, adjusting: [Index]) -> (Self, [Index?])
> 
> This is a very interesting idea. A couple observations:
> 
> 1. The problem of adjusting indices is not just a String one. It also applies to Array, among other things.
> 
> 2. This logic could be encapsulated and reused in a separate type. For instance, imagine:
> 
> 	let myStringProxy = IndexTracking(collection: myString, trackedIndices: [someIndex, otherIndex])
> 	myStringProxy.insert("foo", at: otherIndex)
> 	(someIndex, otherIndex) = (stringProxy.trackedIndices[0], stringProxy.trackedIndices[1])
> 
> Or, with a helper method:
> 
> 	myString.withTracked(&someIndex) { myStringProxy in
> 		myStringProxy.insert("foo", at: otherIndex)
> 	}
> 
> 3. An obstacle to doing this correctly is that a collection's index invalidation behavior is not expressed in the type system. If there were a protocol like:
> 
> 	protocol RangeReplaceableWithEarlierIndexesStableCollection: RangeReplaceableCollection {}
> 
> That would help us here.
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 


I mentioned this much earlier in the thread. My preferred solution would be some kind of RRC-like protocol where mutating methods returned an associated “IndexDisplacement” type. That IndexDisplacement would store, for each operation, the offset and number of index-positions which have been inserted/removed, and know how to translate an index in the previous state in to one in the new state.

You would still need to manually adjust your stored indexes using that IndexDisplacement, but it’d be less error-prone as the logic is written for you.

The standard (non-IndexDisplacement-returning) RRC methods could then be implemented as wrappers which discard the displacement.

- Karl


More information about the swift-evolution mailing list