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

Dan Stenmark daniel.j.stenmark at gmail.com
Mon Feb 13 18:03:22 CST 2017

(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!


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

More information about the swift-evolution mailing list