[swift-evolution] Proposal: Auto-convert for numbers when safe

Jonathan Hull jhull at gbis.com
Sat Dec 5 06:27:50 CST 2015


I understand why you can’t auto-convert from a Double to a Float or Int32 to Int8.  It is good that we have to add the cast explicitly and think though the implications.

…but I don’t think through the implications because we currently have a boy who cried wolf situation where we have to explicitly cast everything (even the safe stuff).


I think all of the numeric types should be able to auto-convert if the conversion is safe (without loss of precision or overflow).

For example:
• If an Int is casting to a larger size (Int16 -> Int32)
• Float -> Double
• Float -> CGFloat
• Int -> Float, Double, or CGFloat (but not the other way)

I don’t see why these aren’t allowed. The forced casts make my code much less readable.  Are the casts above dangerous in a way I am not aware of?

On a side note, can we get a version of floor() which returns an Int (or have I missed that somewhere)?

Thanks,
Jon


More information about the swift-evolution mailing list