<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div> <br> <div id="bloop_sign_1498582382169830144" class="bloop_sign"></div> <br><p class="airmail_on">On June 26, 2017 at 5:43:42 PM, Karl Wagner via swift-evolution (<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div style="color: rgb(0, 0, 0); font-family: 'helvetica Neue', helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div>I would support a definition of encodedOffset that removed mention of UTF-16 and phrased things in terms of String.Encoding and code-units. For example, I would like to be able to construct new String indices from a known index plus a quantity of code-units known to represent a sequence of characters:</div><div><br class=""></div></div><blockquote class="clean_bq" style="font-family: 'helvetica Neue', helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div><div><font face="Menlo" class="">var stringOne = “Hello,“</font></div></div><div><div><font face="Menlo" class="">let stringTwo = “ world"</font></div></div><div><div><font face="Menlo" class=""><br class=""></font></div></div><div><div><font face="Menlo" class="">var idx = stringOne.endIndex</font></div></div><div><div><font face="Menlo" class="">stringOne.append(contentsOf: stringTwo)</font></div></div><div><div><font face="Menlo" class="">idx = String.Index(encodedOffset: idx.encodedOffset + stringTwo.codeUnits.count)</font></div></div><div><div><font face="Menlo" class="">assert(idx == stringOne.endIndex)</font></div></div></blockquote><br class="Apple-interchange-newline"></div></span></blockquote><br><div>I second this concern. We currently use a non-Foundation library that prefers UTF8 encoding, I think UTF8-backed strings are important.</div><div><br></div><div>The choice of UTF16 as string storage in Swift makes historical sense (e.g. runtime interop with ObjC-backed strings) but as Swift moves forward it makes less sense. We need a string system that behaves more like a lightweight accessor for the underlying storage (e.g. if you like your input's encoding you can keep it) unless you do something (like peruse a view) that requires promotion to a new format. That's a different proposal, but that's the direction I'd like to see us head.</div><div><br></div><div>This proposal is in many ways the opposite of that, it specifies that we standardize on UTF16, and in particular we have in view the problem of file archiving (where we would have long-term unarchival guarantees) that complicate backing this out later. This feels like a kludge to support Foundation. In the archive context the offset should either be "whatever the string is" (which you would have to know anyway to archive/unarchive that string) or a full-fledged offset type that specifies the encoding such as</div><div><br></div><div>let i = String.Index (</div><div> encoding: .utf16</div><div> offset: 36</div><div>)</div><div><br></div><div>the latter of which would be used to port an Index between string representations if that's a useful feature.</div><div><br></div><div>More broadly though, I disagree with the motivation of the proposal, specifically</div><div><br></div><div><blockquote type="cite" style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; padding-left: 5px; border-left-width: 1px !important; border-left-color: rgb(0, 64, 128) !important;">The result is a great deal of API surface area for apparently little gain in ordinary code</blockquote></div><div><br></div><div>In ordinary code, we work with a single string representation (e.g. in Cocoa it's UTF16), and there is a correspondence between our UTF16 offset and our UTF16 string such that index lookups will succeed. When we collapse indexes, we lose the information to make this correspondence, which were previously encoded into the typesystem. So the "gain in ordinary code" is that programmers do not have to sprinkle `!` in the common case of string index lookups because we can infer at compile time from the type correspondence it is unnecessary.</div><div><br></div><div>Under this proposal, they will have to sprinkle the `!`, which adds friction and performance impact (`!` is a runtime check, and UTF16 promotions are expensive). I don't believe the simplicity of implementing archival (which one has to only write once) is worth the hassle of complicating all string index lookups.</div><div><br></div><div><div><blockquote type="cite" style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; padding-left: 5px; border-left-width: 1px !important; border-left-color: rgb(0, 64, 128) !important;">Does this proposal fit well with the feel and direction of Swift?</blockquote></div></div><div><br></div><div>To me, one of Swift's greatest strengths is the type system. We can encode information into the type system and find our bugs at compile time instead of runtime.</div><div><br></div><div>Here, we are proposing to partially erase a type because it's annoying to write code that deals with string encodings. But our code will deal with string encodings somehow whether `utf16` appears in our sourcecode or not.</div><div><br></div><div>When we erase the type of our offset, we lose a powerful tool to prove the correctness of our string encodings, that is, the compiler can check our utf16 offset is used with a utf16 string. Without that tool, we either have to check that dynamically, or, worst case, there are bugs in our program.</div><div><br></div><div>Under this proposal we would encourage the use of a bare-integer offsets for string lookup. That does not seem Swifty to me. A Swifty solution would be to add a dynamically-checked type-erased String.Index alongside the existing statically-checked fully-typed String.UTF8/16View.Index so that the programmer can choose the abstraction with the performance/simplicity behavior appropriate for their problem.</div><div><br></div></body></html>