[swift-evolution] [Review] SE-0104: Protocol-oriented integers
jordan_rose at apple.com
Fri Jun 24 17:44:02 CDT 2016
> On Jun 24, 2016, at 15:03, Nicola Salmoria via swift-evolution <swift-evolution at swift.org> wrote:
> On Fri, Jun 24, 2016 at 11:45 PM, Max Moiseev <moiseev at apple.com <mailto:moiseev at apple.com>> wrote:
>> However, division by 0 isn't an overflow: it's an undefined operation. I find it somewhat surprising that dividedWithOverflow/remainderWithOverflow allow attempting this operation.
> I tried to say that in my previous email. I agree that division by zero is NOT the overflow and should probably be handled differently if only for a better error message.
>> To me, the intuitive semantics of the WithOverflow methods are "perform the operation, and if the result doesn't fit in the given type, return a truncated result and an overflow flag". This is not what happens when dividing by 0, because the result simply doesn't exist.
>> I think I would prefer if rhs != 0 was documented as an explicit precondition of the division and remainder operations, and dividedWithOverflow/remainderWithOverflow trapped because of precondition failure.
> That’s exactly how it is implemented in the prototype now.
> Now I'm confused. Isn't this line of the gyb returning .overflow when the divisor is 0?
> https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb#L793 <https://github.com/apple/swift/blob/master/test/Prototypes/Integers.swift.gyb#L793>
> Also, the current version of Swift doesn't trap either:
> let z = 0
> print(Int.divideWithOverflow(1, z)) // (0, true)
> print(Int.remainderWithOverflow(1, z)) // (0, true)
> interestingly, you need to put 0 in a variable otherwise the compiler rejects the lines.
Huh. My bad for not checking. Yeah, I don't think dividing by zero and dividing INT_MIN by -1 are the same kind of overflow.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution