|
1 | 1 | # OverflowContexts.jl
|
2 | 2 |
|
3 | 3 | This package conceptually extends `CheckedArithmetic.jl` to provide the following overall features:
|
4 |
| -1. Ability to set the global default to overflow-checked or overflow-permissive operations. |
| 4 | +1. Ability to set a Module-level default to overflow-checked or overflow-permissive operations. |
5 | 5 | 2. Ability to specify whether a block of code should use overflow-checked or overflow-permissive operations regardless of the default.
|
6 | 6 |
|
7 | 7 | Together, these provide checked and unchecked contexts, as in other languages like C#:
|
8 | 8 | https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked
|
9 | 9 |
|
10 |
| -It's important to know how these interact with Julia. When you set a default through `@default_checked` and `@default_unchecked`, the function definitions |
11 |
| -for the `+`, `-`, and `*` operators, and `abs` function, are being redirected. As those methods are new, all methods that are eligible for being recompiled will do so to |
12 |
| -incorporate the new method definitions. Some methods may not be recompiled, and may not adopt the new methods. This may also cause a long delay when running |
13 |
| -code the first time after these are set. Thus, I recommended that you only rarely change the default context. |
| 10 | +`@default_checked` and `@default_unchecked` create shadow copies of the `+`, `-`, `*`, and `abs` functions that redirect to overflow-checked |
| 11 | +or overflow-permissive operations, respectively, within the module it was executed in. All non-integer arguments are passed through to their |
| 12 | +respective Base methods. |
14 | 13 |
|
15 |
| -The expression-level `@checked` and `@unchecked` work by rewriting the `+`, `-`, and `*` operators, and `abs` function, to methods specific to the checked or permissive operation, and thus are not affected by switching the default. |
16 |
| - |
17 |
| -**NOTE:** If you set `@default_checked`, some operations that expect the Julia default of unchecked arithmetic may not work. Particularly hash functions. |
18 |
| -I've included here a number of such core Julia functions and applied the `@unchecked` macro to them so that they still work. If you encouter an error in |
19 |
| -Julia running in a checked context, please report it as an Issue to this repository so the function can be included here. If the error is in a package, |
20 |
| -you may need to provide your own patch locally. Unless it is a commonly used package, in which case I can conditionally load it here. |
21 |
| - |
22 |
| -Ideally, if this model were to be adopted by Julia itself, such packages could be updated to include functions annotated with `@unchecked` for compatibility |
23 |
| -with the default set to do overflow checking. |
| 14 | +The expression-level `@checked` and `@unchecked` rewrite instances of `+`, `-`, and `*`, and `abs` functions, to functions specific to the |
| 15 | +checked or permissive operation, and thus are not affected by switching the default. Symbols for the functions will also be replaced, to support |
| 16 | +calls like `foldl(+, v)`. If these macros are nested, the lowest level takes precedence so that an unchecked context can be nested inside a checked |
| 17 | +context and vice versa. |
24 | 18 |
|
25 | 19 | ```julia
|
26 | 20 | using OverflowContexts
|
|
0 commit comments