[swift-evolution] floating point numbers implicit conversion

David Sweeris davesweeris at mac.com
Sat Jun 17 23:01:31 CDT 2017

> On Jun 17, 2017, at 20:43, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> In Swift, all types and all operators are implemented in the standard library. How do you express the idea that, when you add values of disparate types T and U, the result should be of the type with greater precision? You need to be able to spell this somehow.

Oh, ok... I thought you meant "conditional conformance" or something concrete :-D

Off the top of my head, with "literals as generic parameters",
protocol Addable {
  associatedtype BitsOfPrecision: IntegerLiteral
  static func + <T: Addable> (_: Self, _: T) -> T where T.BitsOfPrecision >
  static func + <T: Addable> (_: Self, _: T) -> Self where T.BitsOfPrecision <= BitsOfPrecision

Although, come to think of it, I suppose that's a bit more than simply using literals as types. Still, it's all information that's available at compile time, though.

- Dave Sweeris 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170617/272ec8f1/attachment.html>

More information about the swift-evolution mailing list