[swift-evolution] Warning for possible overflow or optional

Stéphane Lizeray stephane at lizeray.net
Tue Feb 16 12:19:59 CST 2016


Safety does not mean you can easily write code that crashes once it is deployed…. 
 
Safety means that the compiler helps you to avoid programming errors as much as possible. This is what Optional is all about.

In this case, I should have used an Int32 instead of converting an UInt16 to an Int16….

Int16(UInt16:) should be either a failable constructor or the compiler should emit a warning.


Stéphane




> On 16 Feb 2016, at 19:02, David Turnbull <dturnbull at gmail.com> wrote:
> 
> 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).
> 
> 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.
> 
> -david  https://github.com/AE9RB/SwiftGL <https://github.com/AE9RB/SwiftGL>
> 
> On Tue, Feb 16, 2016 at 4:45 AM, Stéphane Lizeray <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Hello,
> 
> 
> Don’t you think that the compiler should emit a warning for a possible overflow in the following code?
> 
> 
> func foo(x:UInt16) {
> 
> 	let y = Int16(x)		<— Shouldn’t we have a warning here? Or an optional?
> 	print(y)
> }
> 
> This code is very very fragile and Swift is supposed to bring safety?
> 
> 
> Thanks,
> 
> 
> Stéphane
> 
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160216/803c2ddd/attachment.html>


More information about the swift-evolution mailing list