[swift-evolution] Proposal: failable numeric conversion initializers
Dmitri Gribenko
gribozavr at gmail.com
Sun Dec 6 17:55:34 CST 2015
On Sun, Dec 6, 2015 at 11:28 AM, Matthew Johnson via swift-evolution
<swift-evolution at swift.org> wrote:
> Problem:
> Swift numeric types all currently have a family of conversion initializers. In many use cases they leave a lot to be desired. Initializing an integer type with a floating point value will truncate any fractional portion of the number. Initializing with an out-of-range value traps.
>
> Solution:
>
> Sometimes it would be more desirable to convert the runtime value if it can be done without losing information (or possibly with only minimal loss of precision when initializing a floating point type). This could be easily accomplished if the standard library had a family of failable initializers for all numeric types, either returning an Optional or throwing when the initialization was not successful.
>
> I prefer the throwing version because failure can be automatically propagated up the call stack and the error could capture value that was provided and the type that failed to initialize which may be useful when debugging. Also, `try?` allows callers to throw away the error if the detail isn’t necessary. However, the Optional version would provide the basic functionality that is desired and would be sufficient if the community likes it better.
>
> // Conversions from all integer types.
> init?(_ value: Int8)
> init?(_ value: Int16)
> init?(_ value: Int32)
> init?(_ value: Int64)
> init?(_ value: Int)
> init?(_ value: UInt8)
> init?(_ value: UInt16)
> init?(_ value: UInt32)
> init?(_ value: UInt64)
> init?(_ value: UInt)
One issue is that these initializers already exist with the signature
'init(_ value: IntXYZ)'. Adding these failable initializers would
make code like the following ambiguous:
var u8 = getUInt8()
var myInt = Int(u8)
We need a label to distinguish these.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-evolution
mailing list