<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="direction: inherit;">+1</div><div style="direction: inherit;"><br></div><div style="direction: inherit;">"Explicit is better than implicit"</div><br><div>Daniel Duan</div>Sent from my iPhone</div><div><br>On Jul 21, 2016, at 9:34 AM, Björn Forster via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>+1<div><br></div><div>I think this helps making Swift code more robust and should be included in Swift 3.</div><div><br></div><div>The scenario described by Xin is a real world<span></span> and not an academic one.</div><div><br></div><div>Also, the change he proposes is a very small one, so you get "much bang for the bucks" in my point of view.</div><div><br></div><div>Björn</div><div><br>Am Dienstag, 19. Juli 2016 schrieb Xin Tong via swift-evolution :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">Hi, </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">I would like to propose changing unicodescalar initializer to failable. </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">Currently, when you pass an invalid value to the UnicodeScalar initializer the swift stdlib crashes the program by calling _precondition. This is bad if you construct a unicode scalar from unknown input.</font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">As a result. I would like to propose to mark the initializer as failable and return nil in case of a failure. </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times"><br></font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">Currently, in the code below, the stdlib crashes the program by calling _precondition if codepoint is not a valid unicode. </font></span></pre><pre style="line-height:normal"><span style="font-family:Times;font-size:small;white-space:pre-wrap">var string = “" </span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">let codepoint: UInt32 = 55357 // this is invalid </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">let ucode = UnicodeScalar(codepoint) // Program crashes at this point. </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">string.append(code) </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times"><br></font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">After marking the initializer as failable, users can write code like this. And the program will execute fine even codepoint is invalid. </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">var string = "" let codepoint: UInt32 = 55357 // this is invalid</font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">let ucode = UnicodeScalar(codepoint) </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">if ucode != nil { </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2"> string.append(code!)</font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">} else { </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2"> // do something else </font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2">}</font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap"><font face="Times" size="2"><br></font></span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">As the initializer is now failable, it returns an optional, so optional unchaining or forced unwrapping needs to be used. Alternatively, its also possible to leave status quo and force the users to do input </font></span><span style="font-family:Times;font-size:14px;white-space:pre-wrap">checks</span></pre><pre style="line-height:normal"><span style="font-family:Times;font-size:14px;white-space:pre-wrap">before trying to construct a UnicodeScalar. But i feel having failable initializer helps user to write more robust code.</span></pre><pre style="line-height:normal"><span style="white-space:pre-wrap;font-size:14px"><font face="Times">-Xin </font></span></pre></div></blockquote></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>