Friday, January 27, 2017

Swift 4 String Manifesto

Dave Abrahams and Ben Cohen (mailing list):

The first step in improving this [internationalization] situation is to regularize all localized operations as invocations of normal string operations with extra parameters.


When assigning a Substring to a longer-lived variable (usually a stored property) explicitly of type String, a type conversion will be performed, and at this point the substring buffer is copied and the original string’s storage can be released.


With Substring and String being distinct types and sharing almost all interface and semantics, and with the highest-performance string processing requiring knowledge of encoding and layout that the currency types can’t provide, it becomes important to capture the common “string API” in a protocol. Since Unicode conformance is a key feature of string processing in Swift, we call that protocol Unicode[…]


Index interchange between String and its unicodeScalars, codeUnits, and extendedASCII views can be made entirely seamless by having them share an index type (semantics of indexing a String between grapheme cluster boundaries are TBD--it can either trap or be forgiving). Having a common index allows easy traversal into the interior of graphemes, something that is often needed, without making it likely that someone will do it by accident.


In the long run, we should improve Swift string interpolation to the point where it can participate in most any formatting job. Mostly this centers around fixing the interpolation protocols per the previous item, and supporting localization.


This proposal depends on two new features in the Swift language:

  1. Generic subscripts, to enable unified slicing syntax.

  2. A subtype relationship between Substring and String, enabling framework APIs to traffic solely in String while still making it possible to avoid copies by handling Substrings where necessary.

Regarding this last point, note that String is a struct.

Chris Lattner:

I agree with Joe that Swift can learn a lot from Perl 6 grammar’s and we should take the time to do it right.


So why bless regex literals with language support at all? I see several reasons[…]

5 Comments RSS · Twitter

[…] See also: Swift 4 String Manifesto. […]

[…] Chris Lattner ATP Interview, Swift 4 String Manifesto, ABI Stability Deferred Until After Swift 3.0, Looking Back on Swift 3 and Ahead to Swift […]

[…] Swift 4: Synthesizing Equatable and Hashable Conformance, Swift 4: JSON With Encoder and Encodable, Swift 4 String Manifesto, Provide Custom Collections for Dictionary Keys and […]

[…] Swift 4 String Manifesto, Swift ABI Stability […]

Leave a Comment