Safer Integers


#1

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

print(typemax(UInt) + 2)

This returns 1.


#2

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

#3

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.


#4

SaferIntegers

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


#5

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