<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Ben, Dave (you should not read this now, you’re on vacation :o) & Others<div class=""><br class=""></div><div class="">As described in the Swift Standard Library API Reference:</div><div class=""><br class=""></div><div class=""><div style="box-sizing: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: -0.3569999933242798px;" class="">The <code class="code-voice" style="box-sizing: inherit; font-family: 'SF Mono', Menlo, monospace;">Character</code> type represents a character made up of one or more Unicode scalar values, </div><div style="box-sizing: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: -0.3569999933242798px;" class="">grouped by a Unicode boundary algorithm. Generally, a <code class="code-voice" style="box-sizing: inherit; font-family: 'SF Mono', Menlo, monospace;">Character</code> instance matches what </div><div style="box-sizing: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: -0.3569999933242798px;" class="">the reader of a string will perceive as a single character. The number of visible characters is </div><div style="box-sizing: inherit; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: -0.3569999933242798px;" class="">generally the most natural way to count the length of a string.</div><figure id="2844880" style="box-sizing: inherit; margin: -62px 0px 0px; padding: 1.4em 0px 0px; border-top-width: 62px; border-top-style: solid; border-top-color: transparent; position: static; color: rgb(51, 51, 51); font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', Helvetica, Arial, sans-serif; letter-spacing: -0.3569999933242798px;" class=""><div class="formatted-content" style="box-sizing: inherit;"></div></figure></div><div class=""><div class="">The smallest discrete unit we (app programmers) are mostly working with is this</div><div class="">perceived visible character, what else? </div><div class=""><br class=""></div></div><div class="">If that is the case, my reasoning is, that Strings (could / should? ) be relatively simple, </div><div class="">because most, if not all, complexity of Unicode is confined within the Character object and</div><div class="">completely hidden** for the average application programmer, who normally only needs</div><div class="">to work with Strings which contains these visible Characters, right? </div><div class="">It doesn’t then make no difference at all “what’ is in” the Character, (excellent implementation btw) </div><div class="">(Unicode, ASCCII, EBCDIC, Elvish, KlingonIV, IntergalacticV.2, whatever)</div><div class="">because we rely in sublime oblivion for the visually representation of whatever is in</div><div class="">the Character on miraculous font processors hidden in the dark depths of the OS. </div><div class=""><br class=""></div><div class="">Then, in this perspective, my question is: why is String not implemented as </div><div class="">directly based upon an array [Character] ? In that case one can refer to the Characters of the</div><div class="">String directly, not only for direct subscripting and other String functionality in an efficient way. </div><div class="">(i do hava scope of independent Swift here, that is interaction with libraries should be </div><div class="">solved by the compiler, so as not to be restricted by legacy ObjC etc. </div><div class=""><br class=""></div><div class="">** (expect if one needs to do e.g. access individual elements and/or compose graphics directly?</div><div class=""> but for this purpose the Character’s properties are accessible) </div><div class=""><br class=""></div><div class="">For the sake of convenience, based upon the above reasoning, I now “emulate" this in </div><div class="">a string extension, thereby ignoring the rare cases that a visible character could be based </div><div class="">upon more than a single Character (extended grapheme cluster) If that would occur, </div><div class="">thye should be merged into one extended grapheme cluster, a single Character that is. </div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">//: Playground - implement direct subscripting using a Character array</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// of course, when the String is defined as an array of Characters, directly</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">// accessible it would be more efficient as in these extension functions. </span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> count: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">get</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.characters.count</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (n: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> String(Array(</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.characters)[n])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (r: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Range</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">>) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> String(Array(</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.characters)[r])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><p style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> (r: </span><span style="font-variant-ligatures: no-common-ligatures" class="">ClosedRange</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">>) -> </span><span style="font-variant-ligatures: no-common-ligatures" class="">String</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> String(Array(</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.characters)[r])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> test()</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> zoo = </span><span style="font-variant-ligatures: no-common-ligatures" class="">"Koala </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐨</span><span style="font-variant-ligatures: no-common-ligatures" class="">, Snail </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐌</span><span style="font-variant-ligatures: no-common-ligatures" class="">, Penguin </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐧</span><span style="font-variant-ligatures: no-common-ligatures" class="">, Dromedary </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐪</span><span style="font-variant-ligatures: no-common-ligatures" class="">"</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> print(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"zoo has </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">zoo.count</span><span style="font-variant-ligatures: no-common-ligatures" class="">) characters (discrete extended graphemes):"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">..<zoo.count</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(i,zoo[i],separator: </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"="</span><span style="font-variant-ligatures: no-common-ligatures" class="">, terminator:</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">" "</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"\n"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">7</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">9</span><span style="font-variant-ligatures: no-common-ligatures" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">16</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">18</span><span style="font-variant-ligatures: no-common-ligatures" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">26</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">29</span><span style="font-variant-ligatures: no-common-ligatures" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">39</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"images:"</span><span style="font-variant-ligatures: no-common-ligatures" class=""> + zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">6</span><span style="font-variant-ligatures: no-common-ligatures" class="">] + zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">15</span><span style="font-variant-ligatures: no-common-ligatures" class="">] + zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">26</span><span style="font-variant-ligatures: no-common-ligatures" class="">] + zoo[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">39</span><span style="font-variant-ligatures: no-common-ligatures" class="">])</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">test()</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class="">this works as intended and generates the following output: </div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">zoo has 40 characters (discrete extended graphemes):</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">0=K 1=o 2=a 3=l 4=a 5= 6=</span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐨</span><span style="font-variant-ligatures: no-common-ligatures" class=""> 7=, 8= 9=S 10=n 11=a 12=i 13=l 14= 15=</span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐌</span><span style="font-variant-ligatures: no-common-ligatures" class=""> 16=, 17= </span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">18=P 19=e 20=n 21=g 22=u 23=i 24=n 25= 26=</span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐧</span><span style="font-variant-ligatures: no-common-ligatures" class=""> 27=, 28= 29=D 30=r 31=o 32=m </span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">33=e 34=d 35=a 36=r 37=y 38= 39=</span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐪</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Koala </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐨</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Snail </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐌</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Penguin </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐧</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Dromedary </span><span style="line-height: normal; font-family: 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures;" class="">🐪</span></div><div style="margin: 0px; line-height: normal; font-family: 'Apple Color Emoji';" class=""><span style="line-height: normal; font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">images:</span><span style="font-variant-ligatures: no-common-ligatures" class="">🐨🐌🐧🐪</span></div></div><div class=""><br class=""></div><div class="">I don’t know how (in) efficient this method is. </div><div class=""><div class="">but in many cases this is not so important as e.g. with numerical computation.</div></div><div class=""><br class=""></div><div class="">I still fail to understand why direct subscripting strings would be unnecessary,</div><div class="">and would like to see this built-in in Swift asap. </div><div class=""><br class=""></div><div class="">Btw, I do share the concern as expressed by Rien regarding the increasing complexity of the language.</div><div class=""><br class=""></div><div class="">Kind Regards, </div><div class=""><br class=""></div><div class="">TedvG</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""> </div></body></html>