Skip to content

Commit d33a81b

Browse files
authored
Merge pull request #55 from MikeInnes/teh/README
Update README for the new UUID & __init__ syntax
2 parents 7e5bbdc + 670ea5d commit d33a81b

File tree

1 file changed

+70
-25
lines changed

1 file changed

+70
-25
lines changed

README.md

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
### Note: changes in v0.7
1+
### Note: this page is for Julia 0.7 and higher
22

3-
Requires now needs a UUID, and must be called from within your packages `__init__` function. For example:
4-
5-
```julia
6-
function __init__()
7-
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" do_stuff()
8-
end
9-
```
3+
For older versions of Julia, see https://github.com/MikeInnes/Requires.jl/blob/5683745f03cbea41f6f053182461173e236fdd94/README.md
104

115
# Requires.jl
126

@@ -17,34 +11,85 @@ faster, maybe. It supports specifying glue code in packages which will
1711
load automatically when a another package is loaded, so that explicit
1812
dependencies (and long load times) can be avoided.
1913

20-
Usage is as simple as
14+
Suppose you've written a package called `MyPkg`. `MyPkg` has core functionality that it always provides;
15+
but suppose you want to provide additional functionality if the `Gadfly` package is also loaded.
16+
Requires.jl exports a macro, `@require`, that allows you to specify that some code is conditional on having both packages available.
17+
18+
`@require` must be within the [`__init__`](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation-1) method for your module.
19+
Here's an example that will create a new method of a function called `myfunction` only when both packages are present:
20+
21+
```julia
22+
module MyPkg
23+
24+
# lots of code
25+
26+
myfunction(::MyType) = Textual()
27+
28+
function __init__()
29+
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" myfunction(::Gadfly.Plot) = Graphical()
30+
end
31+
32+
end # module
33+
```
34+
35+
The value in the string is Gadfly's UUID; this information may be obtained
36+
by finding the package in the registry ([JuliaRegistries](https://github.com/JuliaRegistries/General) for public packages).
37+
Note that the `Gadfly.Plot` type may not be available when you load `MyPkg`, but `@require`
38+
handles this situation without trouble.
39+
40+
For larger amounts of code you can use `include` as the argument to the `@require` statement:
2141

2242
```julia
23-
media(::MyType) = Textual()
43+
function __init__()
44+
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" include("glue.jl")
45+
end
46+
```
47+
48+
and this will trigger the loading and evaluation of `"glue.jl"` in `MyPkg` whenever Gadfly is loaded.
49+
You can even use
2450

25-
@require Gadfly begin
26-
media(::Gadfly.Plot) = Graphical()
51+
```julia
52+
function __init__()
53+
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" @eval using MyGluePkg
2754
end
2855
```
2956

30-
For larger amounts of code you can also use `@require Package include("glue.jl")`.
31-
The code wrapped by `@require` will execute as soon as the given package is loaded
32-
(which may be immediately).
57+
if you wish to exploit precompilation for the new code.
58+
59+
## Demo
60+
61+
For a complete demo, consider the following file named `"Reqs.jl"`:
3362

3463
```julia
35-
julia> using Requires
64+
module Reqs
3665

37-
julia> @require DataFrames println("foo")
66+
using Requires
3867

39-
julia> using DataFrames
40-
foo
68+
function __init__()
69+
@require JSON="682c06a0-de6a-54ab-a142-c8b1cf79cde6" @eval using Colors
70+
end
4171

42-
julia> @require DataFrames println("bar")
43-
bar
72+
end
4473
```
4574

46-
Note that the package is not imported by default – you need an explicit `using`
47-
statement if you want to use the packages names without qualifying them.
75+
Here's a session that shows how `Colors` is only loaded after you've imported `JSON`:
76+
77+
```julia
78+
julia> include("Reqs.jl")
79+
Main.Reqs
80+
81+
julia> using Main.Reqs
82+
83+
julia> Reqs.Colors
84+
ERROR: UndefVarError: Colors not defined
85+
86+
julia> using JSON
87+
88+
julia> Reqs.Colors
89+
Colors
90+
91+
julia> Reqs.Colors.RGB(1,0,0)
92+
RGB{N0f8}(1.0,0.0,0.0)
93+
```
4894

49-
See [here](https://github.com/one-more-minute/Jewel.jl/blob/b0e8c184f57e8e60c83e1b9ef49511b08c88f16f/src/LightTable/display/objects.jl#L168-L170)
50-
for some more detailed examples.
95+
Note that if `Reqs` were a registered package you could replace the first two commands with `using Reqs`.

0 commit comments

Comments
 (0)