<div dir="ltr">On Tue, Jan 16, 2018 at 4:30 PM, Nevin Brackett-Rozinsky <span dir="ltr">&lt;<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><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-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters&lt;T: <span class="gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_-2832035268431872244gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></p><p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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></div></blockquote><div><br></div><div>Why not constrain it to `BinaryFloatingPoint`? What other types are you trying to use with this function?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"></div><div class="gmail_quote">Of course, “T: FloatingPoint” is an overconstraint in this example, as what I actually want is “T: OrderedField”</div></div></div></blockquote><div><br></div><div>This approach was considered during revision of integer protocols and rejected. You could make your own protocol to which your types of interest then conform, if you&#39;re so interested.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"> 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-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters&lt;T: <span class="gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (inches: <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p>
<p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></p>
<p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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></div></blockquote><div><br></div><div>Yes, you *could* do that.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"></div><div class="gmail_quote">That only works for numbers which don’t overflow the integer literals though.</div></div></div></blockquote><div><br></div><div>Integer literals don&#39;t overflow until 2048 bits. The following compiles just fine:</div><div><br></div><div>func moles&lt;T : FloatingPoint&gt;(particles: T) -&gt; T {<br></div><div>  let N_A: T = 602_214_085_774_000_000_000_000</div><div>  return particles / N_A</div><div>}</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">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-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> moles &lt;T: <span class="gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>&gt; (particles: <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -&gt; <span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></p><p class="gmail-m_-2832035268431872244gmail-p2" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"><span class="gmail-m_-2832035268431872244gmail-Apple-converted-space">    </span></span><span class="gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">let</span><span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> avogadroNumber: </span><span class="gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span><span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> = </span>6_022_140_857<span class="gmail-m_-2832035268431872244gmail-s4" style="color:rgb(0,0,0)"> * </span>100_000_000_000_000</font></p><p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-Apple-converted-space">    </span><span class="gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></p><p class="gmail-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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-m_-2832035268431872244gmail-p1" style="margin:0px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-variant-east-asian: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></div></blockquote><div><br></div><div>You could write:</div><div><br></div><div>func moles&lt;T : FloatingPoint &amp; LosslessStringConvertible&gt;(particles: T) -&gt; T {</div><div>  let N_A = T(&quot;6.02214085774e+23&quot;)!</div><div>  return particles / N_A</div><div>}</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div><div class="gmail-h5"><div class="gmail_quote">On Tue, Jan 16, 2018 at 3:39 PM, Chris Lattner <span dir="ltr">&lt;<a href="mailto:clattner@nondot.org" target="_blank">clattner@nondot.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class="gmail-m_-2832035268431872244gmail-">On Jan 15, 2018, at 11:01 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt; - Can we change the semantics? Maybe, but I doubt ExpressibleByFloatLiteral can be outright replaced. You&#39;re not the first to wonder about how to design an alternative protocol. Dig through the archives and you&#39;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&#39;d rather see Decimal64/128 become standard library types (already working on it), DecimalFloatingPoint become a standard library protocol, and `0.1` become a &quot;decimal literal&quot; (with Float, Double, Float80, and Decimal64/128 all conforming) as distinct from a &quot;float literal&quot; 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-m_-2832035268431872244gmail-HOEnZb"><font color="#888888"><br>
-Chris</font></span></blockquote></div></div></div></div></div>
</blockquote></div><br></div></div>