[swift-evolution] [Pitch] Int / Int returns Quotient-Remainder Tuple

Max Moiseev moiseev at apple.com
Mon Feb 13 18:08:53 CST 2017


FWIW the new integers proposal defines a quotientAndRemainder method for FixedWidthInteger protocol:
https://github.com/apple/swift-evolution/pull/598/files#diff-6ef84b28f2eaeb383265ccbb60650022R785 <https://github.com/apple/swift-evolution/pull/598/files#diff-6ef84b28f2eaeb383265ccbb60650022R785>

This is not the default you’re proposing, however...

Max

> 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!
> 
> Thoughts?
> 
> Dan
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170213/89d448fc/attachment.html>


More information about the swift-evolution mailing list