OverflowContexts.jl - Fine control of integer overflow checking

Spurred on by a new gripe on Slack about an overflowing integer math issue, I revisited my test package for trying to allow Julia to switch between checked an unchecked arithmetic. I realized it essentially already provided the functionality available in e.g. C# with respect to integer checking. So I polished it off and sent it to the Registry!

The way the local contexts work is that I implemented a macro @unchecked to transform operators into calls to unchecked_<op> methods that dispatch to the underlying intrinsics. @checked comes from the existing package CheckedArithmetic.jl, which rewrites the operators to the checked_<op> methods. I brought in all the methods that depended on unchecked arithmetic to work from Julia Base (that I ran into) and marked them with @unchecked (mostly hash functions).

The local contexts don’t cross function boundaries, naturally.

The global default works, only at toplevel, by swapping out the method definitions of the operators.

I’m sure there are a lot of rough spots, so if you try this out, open an issue over at the repository: https://github.com/BioTurboNick/OverflowContexts.jl.

7 Likes

Oh! I figured out how to do this on a Module-level basis. No need to touch any other packages or Base code.

Get overflow checking on the REPL or in your package without messing up Julia.

v0.2.0 heading to registry now.

5 Likes