Missing operators to add different sized integers
| Originator: | wjlafrance | ||
| Number: | rdar://17511478 | Date Originated: | 30-Jun-2014 04:25 PM |
| Status: | Closed | Resolved: | 08-Jul-2014 03:01 PM |
| Product: | Developer Tools | Product Version: | Xcode 6 beta 2 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
The Swift standard library is missing operators for adding different sized integers.
A developer coming from most other languages would expect integer addition to resize the smaller operand to add correctly, with the expression resulting with the type of the larger operand.
Steps to Reproduce:
Consider this example, throwing an error when adding an Int64 with an Int32.
let smallNumber: Int32 = 15
let bigNumber: Int64 = 4294967311
let total = bigNumber + smallNumber
// error: could not find an overload for '+' that accepts the supplied arguments
Expected Results:
The expression would resolve to an Int64 with the value of 4294967326, by implicitly resizing the smaller operand.
Actual Results:
There is no operator in the Swift standard library to add two dislike-sized integers.
Adding this custom operator achieves the expected behavior:
func +(lhs: Int64, rhs: Int32) -> Int64 {
return lhs + Int64(rhs)
}
let total = bigNumber + smallNumber
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!
Response
Engineering has determined that this issue behaves as intended based on the following:
Not allowing mixed-mode arithmetic is a carefully considered decision.
We are now closing this bug report.
If you have questions regarding the resolution of this issue, please update your bug report with that information.
Please be sure to regularly check new Apple releases for any updates that might affect this issue.