[swift-evolution] Make integer conversion initializers failable

Brent Royal-Gordon brent at architechies.com
Fri Feb 12 18:08:59 CST 2016

> It appears that the current integer conversion initializers will either always work, possibly trap, or truncate:
>> init(_: Int)
>> init(_: Int8)
>> init(_: Int16)
>> init(_: Int32)
>> init(_: Int64)
>> init(_: UInt)
>> init(_: UInt8)
>> init(_: UInt16)
>> init(_: UInt32)
>> init(_: UInt64)
>> init(truncatingBitPattern: Int)
>> init(truncatingBitPattern: Int16)
>> init(truncatingBitPattern: Int32)
>> init(truncatingBitPattern: Int64)
>> init(truncatingBitPattern: UInt)
>> init(truncatingBitPattern: UInt16)
>> init(truncatingBitPattern: UInt32)
>> init(truncatingBitPattern: UInt64)
> I suggest that we change trapping initializers to failable initializers. Initializers that can't fail (identity, unsigned -> bigger signed/unsigned, signed -> bigger signed) should keep a non-Optional type.

Personally, I would want to see any proposal along these lines also address arithmetic. I've asked for optional arithmetic before and the response has basically been that the case is too uncommon to be worth supporting with additional operators and they would prefer to see some way of modifying the behavior of arithmetic operations in a statement. I think these initializers are exactly analogous.

Brent Royal-Gordon

More information about the swift-evolution mailing list