lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Jun 12, 2014 at 7:24 PM, Jay Carlson <nop@nop.com> wrote:
Apple's programming language Swift is defined to raise an error on integer overflow:

"Unlike arithmetic operators in C, arithmetic operators in Swift do not overflow by default. Overflow behavior is trapped and reported as an error. To opt in to overflow behavior, use Swift’s second set of arithmetic operators that overflow by default, such as the overflow addition operator (&+). All of these overflow operators begin with an ampersand (&)."[1]

I wonder how much Apple's security experience shaped this feature. I also wonder if people are disabling it via the "-Ofast" compiler switch...

In any case, it's a notable exception in language design to "ints shall behave like C"--whatever that really means, anyway. :-)

Jay

[1]: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html

It all depends on the application. A web server probably wants to raise exceptions on overflow so it can be certain it's handling things correctly. A game probably wants to just do whatever's fastest which is probably to silently wrap on overflow. A crypto library might want to raise exceptions in some cases, but some crypto algorithms expect wrap on overflow.

The idea of &+ operators looks a bit ugly to me though (and has potential for confusion from people who are used to working with pointers). If I were designing this feature, I think I'd want to have a keyword, or a function such as debug.setoverflowtrap(), which disables overflow trapping for a block or until it's enabled again.

--
Sent from my Game Boy.