Skip to content

Commit 989a576

Browse files
committed
Module-level default, no more compat!
1 parent 25ee2e0 commit 989a576

10 files changed

+77
-954
lines changed

Project.toml

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
name = "OverflowContexts"
22
uuid = "649716ba-0eb1-4560-ace2-251185f55281"
33
authors = ["Nicholas Bauer <[email protected]>"]
4-
version = "0.1.0"
5-
6-
[deps]
7-
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
4+
version = "0.2.0"
85

96
[compat]
107
julia = "1.6"
11-
Requires = "1.1.3"
128

139
[extras]
14-
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
15-
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
16-
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
1710
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1811

1912
[targets]

README.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
# OverflowContexts.jl
22

33
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.
55
2. Ability to specify whether a block of code should use overflow-checked or overflow-permissive operations regardless of the default.
66

77
Together, these provide checked and unchecked contexts, as in other languages like C#:
88
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked
99

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.
1413

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.
2418

2519
```julia
2620
using OverflowContexts

src/OverflowContexts.jl

-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
module OverflowContexts
22
__precompile__(false)
33

4-
using Requires
5-
64
include("macros.jl")
75
include("base_ext.jl")
8-
include("unchecked_compat.jl")
9-
10-
function __init__()
11-
@require SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce" include("SHA_compat.jl")
12-
@require Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" include("Random_compat.jl")
13-
@require Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" include("Revise_compat.jl")
14-
end
156

167
export @default_checked, @default_unchecked, @checked, @unchecked,
178
unchecked_neg, unchecked_add, unchecked_sub, unchecked_mul, unchecked_negsub, unchecked_abs,

src/Random_compat.jl

-32
This file was deleted.

src/Revise_compat.jl

-25
This file was deleted.

src/SHA_compat.jl

-139
This file was deleted.

0 commit comments

Comments
 (0)