[swift-evolution] Improve Number Type

James Campbell james at supmenow.com
Wed Jan 6 17:32:01 CST 2016


There is a number of deficiencies with Swift and Numbers. Especially when
trying to be clever with generics.

- The API exposes built in types like`BuiltIn.Word` as the return type of
some APIs but there is no way to use said types.
- We have a Float and a Float80. Float and Doubles can't be initialised
(via init) with anything but Integer values but Float80 can be initialized
with both Floating Point and Integer
- We have a SignedNumberType but no UnsignedNumberType, All the floating
points use this protocol, so shouldn't this be NumberType ?
- SignedIntegerType and UnsignedIntegerType have a way of generically
constructing an Int as long as you cast it to the Maximum Int or UInt. But
there is no such thing for Floating Point Numbers.
- We have Float80 and a typealised Float32 and Float64. Why couldn't  have
triple as an extra type name, as having one true type with a number is
confusing ?
- If all number types could be initilized with all number types, then a lot
of the constructors could be in the NumberType protocol.

Just in general it was hard to extend integers and floating point types.
For example in my code I wanted to pass a struct that contained a value to
any Floating Point or Integer and it would convert.

This is what I had to do, which is far too much and even sure I'm not
convinced it will work in every condition.

*protocol Box: CustomStringConvertible, CustomDebugStringConvertible {*



*    typealias BoxType = Any*



*    var value: BoxType { get set }*



*    init()*

*    init(_ value: BoxType)*

*}*


*protocol AngleType: Box {*

*    typealias BoxType = Double*

*}*

*//Mark:- Box - FloatingPointType Conversion*


*extension SignedIntegerType {*



*    init<T: Box where T.BoxType == Float80>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Double>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Float>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int8>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int16>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int32>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int64>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt8>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt16>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt32>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt64>(_ value: T) {*

*        self.init(IntMax(value.value))*

*    }*

*}*


*extension UnsignedIntegerType {*



*    init<T: Box where T.BoxType == Float80>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Double>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Float>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int8>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int16>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int32>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == Int64>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt8>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt16>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt32>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*



*    init<T: Box where T.BoxType == UInt64>(_ value: T) {*

*        self.init(UIntMax(value.value))*

*    }*

*}*


*extension Float*

*{*

*    init<T: Box where T.BoxType == Float>(_ value: T)*

*    {*

*        self.value = value.value.value*

*    }*

*}*


*extension Double*

*{*

*    init<T: Box where T.BoxType == Double>(_ value: T)*

*    {*

*        self.value = value.value.value*

*    }*

*}*


*extension Float80*

*{*

*    init<T: Box where T.BoxType == Float80>(_ value: T)*

*    {*

*        self.init(value.value)*

*    }*

*}*


*extension FloatingPointType {*



*    init<T: Box where T.BoxType == Int>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == Int8>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == Int16>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == Int32>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == Int64>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == UInt>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == UInt8>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == UInt16>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == UInt32>(_ value: T) {*

*        self.init(value.value)*

*    }*



*    init<T: Box where T.BoxType == UInt64>(_ value: T) {*

*        self.init(value.value)*

*    }*

*}*

-- 
 Wizard
james at supmenow.com
+44 7523 279 698
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160106/81e3504f/attachment-0001.html>


More information about the swift-evolution mailing list