<div dir="ltr">On Wed, Jan 17, 2018 at 4:56 PM, Jonathan Hull via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I’m with Nevin on this one. Perhaps the easiest thing to do is to add something to the FloatLiteral type that lets you get it as a string if desired.<div><br></div><div>Didn’t we have a discussion a while back on how to make Integer Literals work with BigInt? Maybe there is an idea from that discussion that would help.</div><div><br></div><div>Tl;dr: Literals shouldn’t be tied to a particular implementation of a single conforming type (though they can/should be optimized for common implementations). The issue here is that FloatLiteral is throwing out information which is given to it based on its underlying implementation. I view this as a bug.</div></div></blockquote><div><br></div><div>I suppose you can view it as anything you want; but the initial question was whether there is a reason that FloatingPoint doesn't conform to ExpressibleByFloatLiteral, and there very much is: namely, the current semantics of a float literal.</div><div><br></div><div>Literals aren't tied to any particular conforming type, but they are always tied to some group of built-in types. Until there's a clear additional use case (e.g., a Decimal type), it's pretty pointless to redesign literal protocols, because whether or not a particular way in which information about the literal value is conveyed to the initializer is ergonomic and efficient will depend on the underlying implementation of the type.</div><div><br></div><div>BigInt will be able to take advantage, in the next version of Swift, to DoubleWidth's conformance to _ExpressibleByBuiltinIntegerLiteral; in other words, literals of up to 2048 bits are supported, and that's plenty for a literal.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>Thanks,</div><div>Jon<br><div><br></div><div> <br><div><blockquote type="cite"><div><div class="h5"><div>On Jan 16, 2018, at 4:20 PM, Nevin Brackett-Rozinsky via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_-8372930802263701790Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr"><div>On Tue, Jan 16, 2018 at 6:31 PM, Xiaodi Wu <span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>></span> wrote:<br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span class="m_-8372930802263701790gmail-">On Tue, Jan 16, 2018 at 4:30 PM, Nevin Brackett-Rozinsky <span dir="ltr"><<a href="mailto:nevin.brackettrozinsky@gmail.com" target="_blank">nevin.brackettrozinsky@gmail.<wbr>com</a>></span> wrote:<br></span><div class="gmail_extra"><div class="gmail_quote"><span class="m_-8372930802263701790gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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"><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">2.54</span> * inches<span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s5" style="color:rgb(0,132,0)">// Error</span></font></div><p class="m_-8372930802263701790gmail-m_4983080151436475035gmail-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;background-color:rgb(255,255,255)">
</p><div 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;background-color:rgb(255,255,255)"><font size="1">}</font></div></div></div></div></blockquote><div><br></div></span><div>Why not constrain it to `BinaryFloatingPoint`? What other types are you trying to use with this function?</div></div></div></div></blockquote><div><br></div><div>We should not ask nor expect people to constrain their generic algorithms to BinaryFloatingPoint unless they are working with the radix.<br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="m_-8372930802263701790gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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"><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> centimeters<T: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (inches: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> (<span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">254</span> / <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4" style="color:rgb(39,42,216)">100</span>) * inches</font></div><div 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;background-color:rgb(255,255,255)"><font size="1">}</font></div></div></div></div></blockquote><div><br></div></span><div>Yes, you *could* do that.</div></div></div></div></blockquote><div><br></div><div>And it seems I *will* be doing that, as long as such a workaround is necessary. Though it does appear to have the unfortunate cost of an extra division operation.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="m_-8372930802263701790gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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></span><div>Integer literals don't overflow until 2048 bits. The following compiles just fine:</div><span class="m_-8372930802263701790gmail-"><div><br></div><div>func moles<T : FloatingPoint>(particles: T) -> T {<br></div></span><div> let N_A: T = 602_214_085_774_000_000_000_00<wbr>0</div><div> return particles / N_A</div><span class="m_-8372930802263701790gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div><div><div>When I write that in a playground it shows N_A as 1.67866967797794e+18.</div><div><br></div><div>(Also, you appear to have mistakenly concatenated the standard uncertainty in the last 2 digits, “74”, onto the accepted value for the constant.)</div></div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="m_-8372930802263701790gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid 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"><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">func</span> moles <T: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s2" style="color:rgb(112,61,170)">FloatingPoint</span>> (particles: <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span>) -> <span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span> {</font></div><div 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="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span></span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">let</span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4"> avogadroNumber: </span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s3" style="color:rgb(79,129,135)">T</span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4"> = </span>6_022_140_857<span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s4"> * </span>100_000_000_000_000</font></div><div 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;background-color:rgb(255,255,255)"><font size="1"><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-Apple-converted-space"> </span><span class="m_-8372930802263701790gmail-m_4983080151436475035gmail-m_-2832035268431872244gmail-s1" style="color:rgb(186,45,162)">return</span> particles / avogadroNumber</font></div><p class="m_-8372930802263701790gmail-m_4983080151436475035gmail-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;background-color:rgb(255,255,255)">
</p><div 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;background-color:rgb(255,255,255)"><font size="1">}</font></div></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></span><div>You could write:</div><div><br></div><div>func moles<T : FloatingPoint & LosslessStringConvertible>(par<wbr>ticles: T) -> T {</div><div> let N_A = T("6.02214085774e+23")!</div><div> return particles / N_A</div><span class="m_-8372930802263701790gmail-"><div>}</div></span></div></div></div></blockquote><div><br></div><div>…or I could write “T: FloatingPoint & ExpressibleByFloatLiteral”. I could even make a typealias for that. But I shouldn’t have to.</div><div><br></div><div>Nevin</div></div></div></div></div></div>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>