[swift-evolution] protocol-oriented integers (take 2)

Max Moiseev moiseev at apple.com
Thu Feb 9 12:42:08 CST 2017

Hi Ben,

The reason to include init?<T : BinaryInteger>(exactly:T) as a requirement for the Arithmetic (now Number) protocol was that it already refines ExpressibleByIntegerLiteral, so it would be quite weird to *not* allow explicit initialization.

Generic conversion to floating point types is not covered by this proposal, but is somewhat related:
https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L257 <https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L257>
https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L1588 <https://github.com/apple/swift/blob/master/stdlib/public/core/FloatingPoint.swift.gyb#L1588>
There are a few unimplemented (but planned) initializers on FloatingPoint and BinaryFloatingPoint that would make this happen.
If and when those are implemented, you would be able to write the version of numericCast<I : BinaryInteger, F : BinaryFloatingPoint>() using those.

Hope this answers your question.


> On Jan 17, 2017, at 9:43 AM, Ben Rimmington <me at benrimmington.com> wrote:
> <https://gist.github.com/moiseev/62ffe3c91b66866fdebf6f3fcc7cad8c>
> 	public protocol Arithmetic {
> 	  init?<T : BinaryInteger>(exactly source: T)
> 	}
> 	public protocol BinaryInteger : Arithmetic {
> 	  init?<T : FloatingPoint>(exactly source: T)
> 	}
> Should the `init?(exactly:)` initializers belong to the same protocol?
> Would this allow generic conversions between integer and floating-point types?
> For example, a failable `numericCast` function.
> -- Ben

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

More information about the swift-evolution mailing list