<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">The thing that is “broken” here is generic programming. If I constrain something to FloatingPoint, I cannot use a float literal in calculations with it:</div><div class="gmail_quote"><br></div><div class="gmail_quote">
<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-Apple-converted-space"> </span><span class="gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="gmail-Apple-converted-space"> </span><span class="gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)">
</p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div><div class="gmail_quote"><br></div><div class="gmail_quote">Of course, “T: FloatingPoint” is an overconstraint in this example, as what I actually want is “T: OrderedField” so that eg. a Rational type could be used. And that gives a hint as to the workaround:</div><div class="gmail_quote"><br></div><div class="gmail_quote">
<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p>
<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-Apple-converted-space"> </span><span class="gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></p>
<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div><div class="gmail_quote"><br></div><div class="gmail_quote">That only works for numbers which don’t overflow the integer literals though. If we want a really large or small value then we have to split it in pieces:</div><div class="gmail_quote"><br></div><div class="gmail_quote">
<p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-s1" style="color:rgb(186,45,162)">func</span> moles <T: <span class="gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (particles: <span class="gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-p2" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(39,42,216);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-s4" style="color:rgb(0,0,0)"><span class="gmail-Apple-converted-space"> </span></span><span class="gmail-s1" style="color:rgb(186,45,162)">let</span><span class="gmail-s4" style="color:rgb(0,0,0)"> avogadroNumber: </span><span class="gmail-s3" style="color:rgb(79,129,135)">T</span><span class="gmail-s4" style="color:rgb(0,0,0)"> = </span>6_022_140_857<span class="gmail-s4" style="color:rgb(0,0,0)"> * </span>100_000_000_000_000</font></p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1"><span class="gmail-Apple-converted-space"> </span><span class="gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)">
</p><p class="gmail-p1" style="margin:0px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><font size="1">}</font></p></div><div class="gmail_quote"><br></div><div class="gmail_quote">It would be much nicer to write “let avogadroNumber: T = 6.022140857e23”.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Nevin</div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On Tue, Jan 16, 2018 at 3:39 PM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@nondot.org" target="_blank">clattner@nondot.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On Jan 15, 2018, at 11:01 PM, Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
> - Can we change the semantics? Maybe, but I doubt ExpressibleByFloatLiteral can be outright replaced. You're not the first to wonder about how to design an alternative protocol. Dig through the archives and you'll find some existing ideas. My two cents: The main alternative base in question here is 10. However, decimal storage formats and binary storage formats share so little in common that any initializer common to both will be extremely unwieldy for one or both formats. Personally, somewhere down the road, I'd rather see Decimal64/128 become standard library types (already working on it), DecimalFloatingPoint become a standard library protocol, and `0.1` become a "decimal literal" (with Float, Double, Float80, and Decimal64/128 all conforming) as distinct from a "float literal" that we could then restrict to hexadecimal (?and binary) floating-point literals (and maybe rename accordingly).<br>
<br>
</span>If we were motivated to fix this (and I’m not :-), then I think the best path forward would be to rename ExpressibleByFloatLiteral to something like ExpressibleByBinaryFloatLitera<wbr>l. This would allow the introduction of a new ExpressibleByDecimalFloatLiter<wbr>al with a different initializer requirement.<br>
<br>
I’m not motivated to fix this, because there is nothing actively broken by the current state of things. With the current name we can still introduce a ExpressibleByDecimalFloatLiter<wbr>al someday in the future. The two names will be a little odd, but given the cost of changing it at this point, that seems perfectly acceptable.<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
-Chris</font></span></blockquote></div></div></div>