<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br><div>Sent from my moss-covered three-handled family gradunza</div></div><div><br>On Feb 23, 2017, at 2:04 PM, Ted F.A. van Gaalen <<a href="mailto:tedvgiosdev@gmail.com">tedvgiosdev@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><font size="4" class=""><br class=""></font><div><blockquote type="cite" class=""><div class="">On 23 Feb 2017, at 02:24, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">Equally a non-starter. All known threadsafe schemes that require caches to be updated upon non-mutating operations have horrible performance issues, and further this would penalize all string code by reserving space for the cache and filling it even for the vast majority of operations that don't require random access. </div></div></div></blockquote><div>Well, maybe “caching” is not the right description for what I've suggested.</div><div>It is more like:</div><div> let all strings be stored as they are now, but as soon as you want to work with </div><div>random accessing parts of a string just “lift the string out of normal optimised string storage” </div><div> and then add (temporarily) a Character array so one can work with this array directly ” </div></div></div></blockquote><div><br></div>That's a cache.<div><br><blockquote type="cite"><div><div><div>which implies that all other strings remain as they are. ergo: efficiency </div><div>is only reduced for the “elevated” strings,</div></div></div></blockquote><div><br></div>You have to add that temporary array somewhere. The performance of every string is penalized for that storage, and also for the cost of throwing it out upon mutation. Every branch counts. </div><div><br><blockquote type="cite"><div><div>Using e.g. str.freeSpace(), if necessary, would then place the String back </div><div>in its normal storage domain, thereby disposing the Character array</div><div>associated with it. </div></div></blockquote><div><br></div>Avoiding hidden dynamic storage overhead that needs to be freed is an explicit goal of the design (see the section on String and Substring).<br><br><blockquote type="cite"><div><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">Trust me, we've gotten lots of such suggestions and thought through the implications of each one very carefully. </div></div></div></blockquote><div>That’s good, because it means, that a lot of people are interested in this subject and wish to help. </div><div>Of course you’ll get many of suggestions that might not be very useful, </div><div>perhaps like this one... but sometimes suddenly someone </div><div>comes along with things that might never have occurred to you. </div><div>That is the beautiful nature of ideas…</div></div></blockquote><div><br></div>But at some point, I hope you'll understand, I also have to say that I think all the simple schemes have been adequately explored and the complex ones all seem to have this basic property of relying on caches, which has unacceptable performance, complexity, and, yes, usability costs. Analyzing and refuting each one in detail begins to be a waste of time after that. I'm not really willing to go further down this road unless someone has an implementation and experimental evidence that demonstrates it as non-problematic. </div><div><br></div><div><br><blockquote type="cite"><div><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">I'm afraid you will have to accept being disappointed about this. </div></div></div></blockquote><div><div>Well, like most developers, I am a stubborn kind of guy.. </div><div>Luckily Swift is very flexible like Lego, so I rolled my own convenience struct.</div><div>If I need direct access on a string I simply copy the string to it.</div><div>it permits things like this: (and growing) </div><div><br class=""></div><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: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> strabc = </span><span style="font-variant-ligatures: no-common-ligatures" class="">"abcdefghjiklmnopqrstuvwxyz"</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: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> strABC = </span><span style="font-variant-ligatures: no-common-ligatures" class="">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> abc = </span><span style="font-variant-ligatures: no-common-ligatures" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">strabc</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> ABC = </span><span style="font-variant-ligatures: no-common-ligatures" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">strABC</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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; 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(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// as in Basic: left$, mid$, right$</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">left</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</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><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">mid</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</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><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ABC</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">mid</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</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><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ABC</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">right</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</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=""><br class=""></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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// ranges and concatenation:</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">12</span><span style="font-variant-ligatures: no-common-ligatures" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">23</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" 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><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">left</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">+</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ABC</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">mid</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">6</span><span style="font-variant-ligatures: no-common-ligatures" class="">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">6</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">+</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span><span style="font-variant-ligatures: no-common-ligatures" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">25</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><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(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// eat anything:</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="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> d:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Double</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">-3.14159</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures" class="">(d))</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="">let</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: #272ad8" class="">1234</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures" class="">(n))</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(39, 42, 216);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">1234.56789</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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(0, 132, 0);" 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=""> str = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">15</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">17</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">asString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Copy to to normal Swift String</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(str)</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(0, 132, 0);" 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=""> s = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">12</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">20</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// interpolate to normal Swift String.</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(s)</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(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"34"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// if lengths don't match:</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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">8</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">9</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ABC</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">24</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">25</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// length of dest. string is altered.</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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">12</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"$$$$"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// if src l > 1 will insert remainder after dest.12 here</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; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">14</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">TGString</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">""</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// empty removes character at pos.</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: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</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><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">insert</span><span style="font-variant-ligatures: no-common-ligatures" class="">(at: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures" class="">, string: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ABC</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</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="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" 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><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">abc</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><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(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">test</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></div></div><div>.</div><div>outputs: </div><div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">abcde</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">fghjiklmno</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">FGHIJKLMNOPQRSTUVWXYZ</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">VWXYZ</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">mnopqrstuvw</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">abcdeGHIJKLklmnopqrstuvwxyz</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">-3.14159</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1234</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">1234.56789</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">abcdefghjiklmnopqrstuvwxyz</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">mnopqrst</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">abc34fghYZkl$$$$nopqrstuvwxyz</span></div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">abcABC34fghYZkl$$$$nopqrstuvwxyz</span></div></div><div><br class=""></div></div><div>kinda hoped that this could be builtin in Swift strings </div><div>Anyway, I’ve made myself what I wanted, which happily co-exists</div><div>alongside normal Swift strings. Performance and storage</div><div>aspects of my struct TGString are not very important, because</div><div>I am not using this on thousands of strings.</div><div>Simply want to use a string as a plain array, that’s all, </div><div>which is implemented in almost every PL on this planet. </div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">More generally, there's a reason that the collection model has bidirectional and random access distinctions: important data structures are <u class="">inherently</u> not random access. </div></div></div></blockquote>I don’t understand the above line: definition of “important data structures” <> “inherently” </div></blockquote><div><br></div>Important data structures are those from the classical CS literature upon which every practical programming language (and even modern CPU hardware) is based, e.g. hash tables. Based on the properties of modern string processing, strings fall into the same category. "Inherent" means that performance characteristics are tied to the structure of the data or problem being solved. You can't sort in better than O(N log N) worst case (mythical quantum computers don't count here), and that's been proven mathematically. Similarly it's easy to prove that the constraints of our problem mean that counting the characters in a string will always be O(N) worst case where N is the length of the representation. That means strings are <u>inherently</u> not random access.</div><div><br><blockquote type="cite"><div><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">Heroic attempts to present the illusion that they are randomly-accessible are not going to fly. </div></div></div></blockquote> ?? Accessing discrete elements directly </div></blockquote><div><br></div>All collections have direct access via indices. You mean randomly, via arbitrary integers. </div><div><br><blockquote type="cite"><div>in an array is not an illusion to me. </div><div>(e.g. I took the 4th and 7th eggs from the container) <br class=""></div></blockquote><div><br></div>It's not an illusion when they're stored in an array. </div><div><br></div><div>If you have to walk down an aisle of differently sized cereal boxes to pick the 5th box of SuperBoomCrisp Flakes off the shelf in the grocery store, that's not random access (even if you're willing to drop the boxes into an array for later lookups as you go, as you're proposing). <i>That's</i> what Strings are like. </div><div><br><blockquote type="cite"><div><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">These abstractions always break down, leaking the true non-random-access nature in often unpredictable ways, penalizing lots of code for the sake of a very few use-cases, and introducing complexity that is hard for the optimizer to digest and makes it painful (sometimes impossible) to grow and evolve the library. </div><div class=""><br class=""></div></div></div></blockquote>Is an Array an abstraction? of what? </div></blockquote><div><br></div>A randomly-accessible homogeneous tail growable collection. But the abstraction in question here isn't Array; it's RandomAccessCollection. </div><div><br><blockquote type="cite"><div>I don’t get this either. most components in the real world can be accessed randomly. </div></blockquote><br><div>Actually that's far from being true in the real world. See the grocery store above. Computer memory is very unlike most things in the real world. Ask any roboticist.</div><div><br></div><blockquote type="cite"><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">This should be seen as a general design philosophy: <i class="">Swift presents abstractions that harmonize with, rather than hide, the true nature of things</i>.</div></div></div></blockquote><div>The true nature of things is a very vague and subjective criterium, </div></div></blockquote><div><br></div>Not at all; see my explanation above.</div><div><br><blockquote type="cite"><div><div>how can you harmonise with that, let alone with abstractions? </div><div>e.g. for me: “the true nature of things” for an array is that it has direct accessible discrete elements…</div></div></blockquote><div><br></div>Arrays definitely support random access. Strings are not arrays.</div><div><br><blockquote type="cite"><div><div>Sorry, with respect, we have a difference of opinion here.</div></div></blockquote><div><br></div>That's fine. Please don't be offended that I don't wish to argue it further. It's been an interesting exercise while I'm on vacation and I hoped it would lay out some general principles that would be useful to others in future even if you are not convinced, but when I get back to work next week I'll have to focus on other things. </div><div><br><blockquote type="cite"><div><div>Thanks btw for the link to this article about tagged pointers, very interesting.</div><div>it inspired me to (have) read other things in this domain as well. </div><div><br class=""></div><div>TedvG</div><div><br class=""></div></div><br class=""></blockquote></div></body></html>