<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 14 Feb 2017, at 00:03, Dan Stenmark via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal;" class="">(I get the feeling the response to this pitch will be overwhelming negative, but <i class="">*deep inhale*</i> here I go!)</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">A common mistake I see programmers make is dividing two integers and expecting a floating-point result. This mostly affect new programmers who haven't learned about ALUs yet, but I sometimes even see veterans make the mistake when they don't realize that neither operand they're passing is floating-point.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> foo = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">17</span><span style="font-variant-ligatures: no-common-ligatures" class=""> / </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">foo</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// Huh, why is this 3 and not 3.4? Oh, wait, I'm an idiot.</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I'd like to propose we make '/' operator on two Ints return a quotient-remainder tuple by default. This should help both new and veteran programmers alike write less error-prone code.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (quotient, remainder) = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">17</span><span style="font-variant-ligatures: no-common-ligatures" class=""> / </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">5</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">( </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Q:</span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">quotient</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">) R:</span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">remainder</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// Idiot-proof!</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Thoughts?</div></div></div></blockquote><br class=""></div><div>Hmm, it's an interesting idea; I think it depends on how effectively it would optimise, for example:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = 17 / 5</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let bar = 5 + foo.quotient</font></div><div><br class=""></div><div>Since foo.remainder is never used, this should optimise as:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo:Int = 17 / 5</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let bar:Int = 5 + foo // tuple and .quotient are optimised away</font></div><div><br class=""></div><div>We'd need someone more knowledge on the compiler to weigh in, but it seems like something that should be fairly easy to optimise, otherwise it would have a big impact on performance of any integer heavy operations.</div><div><br class=""></div><div><br class=""></div><div>The obvious question though is how much of a problem is this really?</div><div><br class=""></div><div>I can't recall the last time I might have made a mistake like this, as if I want a float result I always make certain at least one of the operands is a floating point type, and with that in mind having to put .quotient all over the place could be burdensome rather than helpful.</div><div><br class=""></div><div>I wonder if an alternative would simply be to have the compiler/type-checker identify ambiguity. For example:</div><div><br class=""></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo = 17 / 5 // warning: ambiguous expected type</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(foo) // this is no help, as it can take both int or float</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo:Float = 17 / 5 // no warning: type is explicitly defined</font></div><div><font face="Monaco" class=""><br class=""></font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(17 / 5) // warning: ambiguous expected type</font></div><div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(Float(17 / 5)) // no warning: type is explicitly defined</font></div><div><br class=""></div><div>And so-on. Basically we'd have a warning in any case where type-inference cannot determine whether the desired type is integer or float, encouraging developers to specify an explicit type anywhere that there's a risk of a mistake. Of course this puts more of a burden on type-inference, which isn't always the most stable thing to begin with, but it seems like it might be a more elegant solution?</div></body></html>