<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Safety does not mean you can easily write code that crashes once it is deployed…. </div><div class=""> </div><div class="">Safety means that the compiler helps you to avoid programming errors as much as possible. This is what Optional is all about.</div><div class=""><br class=""></div><div class="">In this case, I should have used an Int32 instead of converting an UInt16 to an Int16….</div><div class=""><br class=""></div><div class="">Int16(UInt16:) should be either a failable constructor or the compiler should emit a warning.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Stéphane</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 16 Feb 2016, at 19:02, David Turnbull <<a href="mailto:dturnbull@gmail.com" class="">dturnbull@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This is perfectly safe since it's checked at runtime. If you don't want it checked at runtime you can use Int16(bitPattern: x).<div class=""><br class=""></div><div class="">I just finished writing the inflate algorithm in Swift. With your suggestion I would have used bitPattern or truncatingBitPattern everywhere to bypass warnings and missed at least two times where Swift caught a mistake a runtime. C API users would have a similar problem.</div><div class=""><br class=""></div><div class="">-david <a href="https://github.com/AE9RB/SwiftGL" class="">https://github.com/AE9RB/SwiftGL</a></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Feb 16, 2016 at 4:45 AM, Stéphane Lizeray <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Hello,<div class=""><br class=""></div><div class=""><br class=""></div><div class="">Don’t you think that the compiler should emit a warning for a possible overflow in the following code?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><span style="color:#0433ff" class="">func</span> foo(x:<span style="color:#3495af" class="">UInt16</span>) {</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><span style="white-space:pre-wrap" class="">        </span><span style="color:#0433ff" class="">let</span> y = <span style="color:#3495af" class="">Int16</span>(x)<span style="white-space:pre-wrap" class="">                </span><— Shouldn’t we have a warning here? Or an optional?</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(52,149,175)" class=""><span style="" class=""><span style="white-space:pre-wrap" class="">        </span></span>print<span style="" class="">(y)</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class="">}</div></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class="">This code is very very fragile and Swift is supposed to bring safety?</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class="">Thanks,</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo" class="">Stéphane</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>