[swift-evolution] [pitch] Character.unicodeScalars

Ben Cohen ben_cohen at apple.com
Thu May 11 14:56:22 CDT 2017


> On May 10, 2017, at 11:13 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> On May 10, 2017, at 4:51 PM, Ben Cohen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Add a unicodeScalars property to Character, presending a lazy view of the scalars in the character, along similar lines to the one on String.
>> 
>> 
> 
> Might it make sense conform `Character` itself to `Collection`, rather than using a view?
> 

Hmm. I don’t think this would be right. The composition of Character is not fundamental to its very being (unlike String’s composition, where being of element type Character is an important principle for Swift) – it’s a lower-level thing that a user can poke at for specific purposes. 

Also, one of the discoveries we’ve made while making String a Collection is it has some unfortunate effects on code that uses flatMap inapporpriately. You can use flatMap with a function (Element)->T, and it has the same effect as map because the function is implicitly converted to (Element)->T? and then the elements are unwrapped again by the flatMap. But if you were doing this on String, and then String becomes a Collection, suddenly you get the more appropriate flatMap that flattens nested collections, and you get a [Character] back instead of the expected [String]. We’ve been able to put in compatibility shims to detect this specific case so people can be warned in Swift 3 compatibility mode, but I fear making Character a collection too may itself may introduce even more problems, possibly ones we can’t work around without compiler features. This reason alone might not be enough to rule out making Character a collection in the future, but it probably rules it out for Swift 4.

> Otherwise, I'm in favor. (Though it'd be nice to have *some* way to manipulate the `UnicodeScalar`s inside a `Character`, even if `RangeReplaceableCollection`'s interface would make preserving its invariants too difficult. That could wait, though.)
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170511/7e129272/attachment.html>


More information about the swift-evolution mailing list