[swift-evolution] Implicit Type Conversion For Numerics Where Possible.

Jonathan Hull jhull at gbis.com
Wed Mar 30 18:10:28 CDT 2016


Would a valid stop-gap be to define operators for some of the common cases?

For example:

func * (lhs:Double, rhs:Int)->Double

Are there issues with this approach that I am unaware of?  It seems like the desired cast (and the resulting effect) is obvious there, and you don’t get surprising casts elsewhere.

Basically, the rule (as thought of by the programmer) would be in math which mixes Ints & Doubles, the Int is treated as a double.  You could make a similar rule for Int + CGFloat.

For math on mixed types, the result IMHO should follow this scale of promotion (with the parameter farthest left on the scale also being the result type):

CGFloat <- Double <- Float <- Int

I put CGFloat as the highest because in real-world code you often multiply a CGFloat by a constant Double such as M_PI.  I can’t think of many cases where you have a CGFloat and want anything but a CGFloat in return.

Thanks,
Jon


More information about the swift-evolution mailing list