[swift-evolution] Implicit truncation

Dave Abrahams dabrahams at apple.com
Tue May 30 18:46:00 CDT 2017


on Tue May 30 2017, Greg Parker <gparker-AT-apple.com> wrote:

>> On May 26, 2017, at 12:04 AM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> I've often wondered if even just "bitPattern" might suffice, as the truncating or extending of it
> should not be particularly surprising.
>
> Being explicit about bit pattern truncation or extension is
> valuable. It helps catch bugs where the bit count is not what the
> author expected. In Swift it is especially important for types like
> Int and CGFloat which have platform-dependent sizes.
>
>     let x: Int64 = …
>     let y = Int(bitPattern: x) // if truncation is implicit then this may be surprising on 32-bit
> platforms
>
>     let a: UInt32 = …
>     let b = Int(bitPattern: a) // if zero- or sign-extension is implicit then this may be surprising
> on 64-bit platforms

Yeah, but we now have generic conversion constructors:

    Int(extendingOrTruncating: x)

I think that loses most of the value of the labels.  The only reason
I've ever used this form is for truncation, FWIW.

-- 
-Dave


More information about the swift-evolution mailing list