[swift-evolution] Warning for possible overflow or optional

Shawn Erickson shawnce at gmail.com
Tue Feb 16 12:27:11 CST 2016


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

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.

On Tue, Feb 16, 2016 at 10:20 AM Stéphane Lizeray <swift-evolution at swift.org>
wrote:

>
> 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
>
> On Tue, Feb 16, 2016 at 4:45 AM, Stéphane Lizeray <
> 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
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> 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/75ada48a/attachment.html>


More information about the swift-evolution mailing list