> On Aug 17, 2016, at 2:34 PM, Kenny Leung via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> 
>> William Sumner says:
>> Can you be more specific about the improvements you’d like to see? Based on 
>> an earlier message, you want to be able to use subscripting on strings to 
>> retrieve visual glyphs, but you can do this now via the .characters 
>> property, which presents a view of the string’s contents as a collection of 
>> extended grapheme clusters.
> 
> I did not know about .characters. I would say this addresses the glyph 
> portion of my issues.
> 
> I still have a problem with not being able to index using simple integers to 
> create subscripts or ranges. I wrote my own “split" function, and found it 
> extremely frustrating to have to work with .Index types instead of being able 
> to use integers for subscripts and ranges. Compared to other languages, this 
> almost obviates the usefulness of subscripts altogether. I understand that 
> there are performance implications with translating integer subscripts into 
> actual indexes in the string, but I guess this is a case where even 
> generating another view on the string doesn’t do enough (internally) to make 
> it worthwhile. Perhaps if it did… Again, this is very beginner unfriendly. I 
> guess I will amend my definition of beginner to not only include people new 
> to programming, but people already experienced in languages besides Swift. 
> Now that I think about it, NSString is as powerful as Swift.String when ti 
> comes to Unicode, and it still allows integer based indexing.
> 
> Another issue I have is that a String itself is not subscriptable, but that 
> you have to get a view of it. I think String should have some default 
> subscriptability that “does the right thing”, whatever that is decided to be.
> 
> <heart-to-heart on>
> Now that we’re getting to the heart of the problem (thanks for the prompting 
> me to think more deeply about it), Swift may be more frustrating to learn for 
> experienced programmers coming from C, Objective-C, Java, Ruby, etc. You try 
> to do the simplest think like index into a string, and then find out you 
> can’t - you think to yourself, “I’ve been programming in Objective-C for 20 
> years. Why can’t I do this? Am I stupid? Is the Swift team purposely trying 
> to make this hard for me?”
> 
> I’ve been reading swift-evolution for a long time now, and a reason often 
> given for design decisions is “term of art”. I believe that integer-based 
> subscriptablilty is a term of art that should be supported.
> <heart-to-heart off>

Note that working with individual characters of a NSString can be unsafe 
because a visual glyph may be represented by multiple characters. NSString 
provides methods like rangeOfComposedCharacterSequencesForRange: to enable you 
to align your character retrievals along grapheme cluster boundaries.

You may be interested in this article by Mike Ash, which gives a rationale for 
the String API, including why indexes aren't simple integers: 
https://www.mikeash.com/pyblog/friday-qa-2015-11-06-why-is-swifts-string-api-so-hard.html

In short, these are not simple accesses but potentially expensive operations, 
and integer subscripting could give users the assumption that they’re accessing 
arrays with no performance overhead.

Preston
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to