As far as I'm concerned, Swift and many other strongly typed languages should really go back to math school and learn about number theory. Apart from internal representations (which a high level language should abstract away instead of burden programmers with), all numeric types belong to a domain that is a subset of a larger one.
To compensate for this you can create a protocol that will "understand" that a Real number (Double) belongs to a domain that includes all others. You will then be able to define functions that will accept all numeric types from lower level domains using that protocol and process them with "Real" (Double) operators that will produce a valid result (in theory).
For example:
protocol Numeric
{
var asDouble:Double { get }
}
extension Double:Numeric { var asDouble:Double { return self } }
extension Int:Numeric { var asDouble:Double { return Double(self) } }
extension Int8:Numeric { var asDouble:Double { return Double(self) } }
extension Int16:Numeric { var asDouble:Double { return Double(self) } }
extension Int32:Numeric { var asDouble:Double { return Double(self) } }
extension Int64:Numeric { var asDouble:Double { return Double(self) } }
extension UInt:Numeric { var asDouble:Double { return Double(self) } }
extension UInt8:Numeric { var asDouble:Double { return Double(self) } }
extension UInt16:Numeric { var asDouble:Double { return Double(self) } }
extension UInt32:Numeric { var asDouble:Double { return Double(self) } }
extension UInt64:Numeric { var asDouble:Double { return Double(self) } }
extension Float:Numeric { var asDouble:Double { return Double(self) } }
extension Float80:Numeric { var asDouble:Double { return Double(self) } }
extension NSNumber:Numeric { var asDouble:Double { return Double(self) } }
func foo(_ number: Numeric) -> Double
{
return bar(number.asDouble)
}
Of course this does not take into account precision limitations (e.g. Float vs Double ) and will not actually process numbers from larger domains (irrational or imaginary) but, for all intents and purposes and from a conceptual point of view, it will get the insignificant variations out of the way.
Now the next tedious piece of work will be to implement all math and assignment operators so they can work with the Numeric protocol (but I will leave that to Apple).