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

Ted F.A. van Gaalen tedvgiosdev at gmail.com
Wed Mar 30 07:01:32 CDT 2016


Currently, one has to deal with explicit conversion between numerical types,
which in many cases is unnecessary and costing time to code 
for things that are quite obvious,
and cluttering the source, making it less readable.

Especially dealing all the time with often unavoidable intermixing 
of floating point types CGFloat, Float, and Double 
is really very annoying. 

Conversion beween floating point types is always harmless as 
floating point types are essentially the same. 
They differ only in precision.

Therefore, I would recommend allowing the following implicit type conversions:

-between all floating point types e.g. Double, Float, CGFloat  

-from any integer type to floating point types

-Also, personally, I wouldn’t mind assigning from a float to a (signed) integer
because I know what I am doing: that the fraction is lost 
and that assigning a too large float to an Integer would then cause 
a run time error, which I can try/catch, of course. 

-from unsigned integer to signed integer   
(nothing is lost here, but overflow should cause a run time error) 

but no implicit conversion for:
- from integer to unsigned integer   (loosing sign here)
- from a larger integer type to a smaller one e.g.   Int32 <- Int64   (truncation)  

Note however, that the compiler should issue warnings 
when you do implicit conversions, but these warnings 
are for most programmers of the “Yeah I know, don’t bug me.”
type, so one should be able to switch off these type of warnings.

Even a programmer with little experience simply knows 
that bringing integers into the floating point domain 
causes precision loss. 
He/she also knows that assigning a Double to a smaller floating
point type also cause precision loss.  
the reverse is not true.


Very much interested in your opinion!

----
N.B. the above does not yet include 
the fixed decimal numerical type as this type is not yet
available in Swift.  However, it should be  implemented 
*as soon as possible*  because the fixed decimal type 
is really needed for applications working with financial data!
E.g. 
var depositPromille: Decimal(10,3)
typealias  Money = Decimal(20,2) 
  
For more info on how this could be implemented
in Swift. please read a PL/1 manual, ( i grew up in this world)
like this one: 

http://www.ibm.com/support/knowledgecenter/#!/SSY2V3_4.3.0/com.ibm.entpli.doc_4.3/lr/preface_plugin.htm <http://www.ibm.com/support/knowledgecenter/#!/SSY2V3_4.3.0/com.ibm.entpli.doc_4.3/lr/preface_plugin.htm>

especially under sub-topic “Data elements” 

(however, don’t take everything for granted, PL/1 is still a very young language :o) 
Unfortunately OOP never made it into PL/1 because with it, it would be nearly perfect.)

Should I make a new swift-evolution topic for fixed decimal?

Kind Regards
TedvG

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160330/acde13d7/attachment.html>


More information about the swift-evolution mailing list