[swift-evolution] [Pitch] Int / Int returns Quotient-Remainder Tuple
moiseev at apple.com
Mon Feb 13 18:08:53 CST 2017
FWIW the new integers proposal defines a quotientAndRemainder method for FixedWidthInteger protocol:
This is not the default you’re proposing, however...
> On Feb 13, 2017, at 4:03 PM, Dan Stenmark via swift-evolution <swift-evolution at swift.org> wrote:
> (I get the feeling the response to this pitch will be overwhelming negative, but *deep inhale* here I go!)
> 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.
> let foo = 17 / 5
> print( foo ) // Huh, why is this 3 and not 3.4? Oh, wait, I'm an idiot.
> 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.
> let (quotient, remainder) = 17 / 5
> print( "Q:\(quotient) R:\(remainder)" ) // Idiot-proof!
> swift-evolution mailing list
> swift-evolution at swift.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution