# OverflowContexts.jl - Fine control of integer overflow checking

**URL:** <https://discourse.julialang.org/t/overflowcontexts-jl-fine-control-of-integer-overflow-checking/72273>\
**Category:** Package Announcements\
**Tags:** math, integer-overflow\
**Created:** [November 30, 2021, 6:58am UTC](https://discourse.julialang.org/t/overflowcontexts-jl-fine-control-of-integer-overflow-checking/72273 "2021-11-30T06:58:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [November 30, 2021, 6:58am UTC](https://discourse.julialang.org/t/overflowcontexts-jl-fine-control-of-integer-overflow-checking/72273/1 "2021-11-30T06:58:57Z")

</div>

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](https://github.com/BioTurboNick/OverflowContexts.jl).

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [December 4, 2021, 6:54pm UTC](https://discourse.julialang.org/t/overflowcontexts-jl-fine-control-of-integer-overflow-checking/72273/2 "2021-12-04T18:54:14Z")

</div>

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.
