@@ -7,29 +7,43 @@ that they are not compatible at all. E.g. you cannot
7
7
you as a package developer want to make your package support both Distributed
8
8
and DistributedNext, we suggest using
9
9
[ Preferences.jl] ( https://juliapackaging.github.io/Preferences.jl/stable/ ) to
10
- choose which library to load:
10
+ choose which package to load.
11
+
12
+ Here's an example for a package named Foo.jl:
11
13
``` julia
12
- import Preferences: load_preference
14
+ module Foo
15
+
16
+ import Dependency
17
+ import Preferences: @load_preference , @set_preferences!
13
18
14
- const distributed_library_name = load_preference (" distributed-library " , " name " )
15
- if distributed_library_name == " DistributedNext"
19
+ const distributed_package = @ load_preference (" distributed-package " )
20
+ if distributed_package == " DistributedNext"
16
21
using DistributedNext
17
- elseif distributed_library_name == " Distributed"
22
+ elseif distributed_package == " Distributed"
18
23
using Distributed
19
24
else
20
- error (" Unsupported `distributed-library `: '$(distributed_library_name ) '" )
25
+ error (" Unsupported `distributed-package `: '$(distributed_package ) '" )
21
26
end
22
- ```
23
27
24
- ` distributed-library ` is a top-level name that we recommend all packages who
25
- want to support switching backends use. The advantage of using a global
26
- preference instead of a per-package preference is that you can set it once:
27
- ``` julia
28
- import Preferences: set_preferences!
28
+ """
29
+ set_distributed_package!(value[="Distributed|DistributedNext"])
29
30
30
- set_preferences! (" distributed-library" , " name" => " DistributedNext" )
31
+ Set a [preference](https://github.com/JuliaPackaging/Preferences.jl) for using
32
+ either the Distributed.jl stdlib or DistributedNext.jl. You will need to restart
33
+ Julia after setting a new preference.
34
+ """
35
+ function set_distributed_package! (value)
36
+ # Set preferences for all dependencies
37
+ Dependency. set_distributed_package! (value)
38
+
39
+ @set_preferences! (" distributed-package" => value)
40
+ @info " Foo.jl preference has been set, restart your Julia session for this change to take effect!"
41
+ end
42
+
43
+ end
31
44
```
32
45
33
- And all packages that support the ` distributed-library ` preference will pick
34
- that up automatically so an end-user doesn't need to worry about setting
35
- preferences for all of the dependencies of the package that they want to use.
46
+ Users will then be able to call
47
+ e.g. ` Foo.set_distributed_package!("DistributedNext") ` . Note that
48
+ ` Foo.set_distributed_package!() ` should also set the preferences of any dependencies
49
+ of Foo.jl that use a distributed worker package.
0 commit comments