Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Idiom # 85: Check if integer addition will overflow

See programming-idioms.org:

Code

For all integers with wrap-around semantics (i.e. all the standard types i32, u64, etc.), there are two ways to check for overflow on addition. First, the predicate overflowOnAdd checks if an addition of two values would overflow. Next, there is an addition operator infix +? that performs overflow checking and returns a numOption that can be checked afterwards.

Note that infix + would result in a runtime error in case of an overflow if debug checks are enabled. If overflow semantics are desired, operator infix +° has to be used.







Running Example