[swift-evolution] Strings in Swift 4

Ben Cohen ben_cohen at apple.com
Thu Jan 19 22:18:44 CST 2017


> On Jan 19, 2017, at 19:38, David Sweeris <davesweeris at mac.com> wrote:
> 
> Regarding substrings... Instead of having separate `ArraySlice` and `Substring` types, what about having just one type, `Slice<T: Sequence>`, for anything which shares memory? Seems like it'd be easier for users who'd only have to worry about shared storage for one type, and for stdlib authors who'd only have to write it once.

Collections already do get a default SubSequence implementation of Slice<Base: Indexable> that is essentially like just that. 

The reason types like Array and String have their own is to customize it with more than the default behavior. For example, ArraySlice provides .withUnsafeBufferPointer  method just like an Array does. Substring would need all the features String provides.

Now, once we get conditional conformance, we could use that to maybe increase sharing, for example we could create a protocol for types backed by contiguous memory that provided withUnsafeEtc, and then use conditional conformance to add those features to Slice when the Base has them. This probably won't improve user experience particularly though, just help library authors organize/minimize the code.



More information about the swift-evolution mailing list