Safer Integers

Is it possible to throw an exception automatically when something like this happens:

print(typemax(UInt) + 2)

This returns 1.

There is:

julia> Base.Checked.checked_add(typemax(UInt), 2)
ERROR: OverflowError()
Stacktrace:
 [1] checked_add at ./checked.jl:165 [inlined]
 [2] checked_add(::UInt64, ::Int64) at ./checked.jl:19

You could create a package that defines Integer types that use Base.Checked routines for all arithmetic operations. See RoundingIntegers as a possible starting point.

5 Likes

SaferIntegers

untested – and the floor is open to suggestions while I am here today.

6 Likes

wow, nice!! I think this will be useful.