<div dir="ltr">"Int16(UInt16:) should be either a failable constructor or the compiler should emit a warning."<div><br></div><div>That feels right to me (failable constructor) from a safety standpoint, make you code to deal with it explicitly instead of being surprised by a runtime failure that you make not detect easily in testing, etc.</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Feb 16, 2016 at 10:20 AM Stéphane Lizeray <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>Safety does not mean you can easily write code that crashes once it is deployed…. </div><div> </div><div>Safety means that the compiler helps you to avoid programming errors as much as possible. This is what Optional is all about.</div><div><br></div><div>In this case, I should have used an Int32 instead of converting an UInt16 to an Int16….</div><div><br></div><div>Int16(UInt16:) should be either a failable constructor or the compiler should emit a warning.</div></div><div style="word-wrap:break-word"><div><br></div><div><br></div><div>Stéphane</div></div><div style="word-wrap:break-word"><div><br></div><div><br></div><div><br></div><div><br></div><div><div><blockquote type="cite"><div>On 16 Feb 2016, at 19:02, David Turnbull <<a href="mailto:dturnbull@gmail.com" target="_blank">dturnbull@gmail.com</a>> wrote:</div><br><div><div dir="ltr">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><br></div><div>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><br></div><div>-david <a href="https://github.com/AE9RB/SwiftGL" target="_blank">https://github.com/AE9RB/SwiftGL</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 16, 2016 at 4:45 AM, Stéphane Lizeray <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<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">Hello,<div><br></div><div><br></div><div>Don’t you think that the compiler should emit a warning for a possible overflow in the following code?</div><div><br></div><div><br></div><div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="color:#0433ff">func</span> foo(x:<span style="color:#3495af">UInt16</span>) {</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#0433ff">let</span> y = <span style="color:#3495af">Int16</span>(x)<span style="white-space:pre-wrap">                </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)"><span><span style="white-space:pre-wrap">        </span></span>print<span>(y)</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo">}</div></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo">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"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo">Thanks,</div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo">Stéphane</div><div><br></div><div><br></div><div><br></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>