Swift should support optional arithmetic on integers

Originator:brent
Number:rdar://17428664 Date Originated:23-Jun-2014 05:43 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode6-Beta2 (6A216f)
Classification:Feature (New) Reproducible:Always
 
Summary:
Currently Swift supports two variants of arithmetic operators:

* Regular arithmetic operators, like + and *, cause a runtime error if an operation overflows.
* Overflowing arithmetic operators, like &+ and &*, ignore overflows.

I would like to see a third set of operators, perhaps along the lines of ?+ and ?* (or +? and *?), which return nil if the operation fails.

Steps to Reproduce:
1. In a playground, write an arithmetic operation that’s likely to overflow, e.g.:

    Int.max ?+ Int.max

Expected Results:
A nil value.

Actual Results:
A syntax error. You can choose between a crash or completely ignoring overflows.

Notes:
Optional arithmetic operators should accept either regular or optional types, and always return optional types. If any operand is nil, the result of the operator should also be nil.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!