Skip to content

Commit 984485e

Browse files
tfierspull[bot]
authored andcommitted
manual/workflow-tips: simplify the basic workflow (and explain the why) (JuliaLang#48319)
I came upon this piece of documentation again after seeing this post: https://discourse.julialang.org/t/how-to-clear-variables-and-or-whole-work-space/10149/21?u=tfiers ("How to clear variables and/or whole work space?" → "Work in a module") This small addendum to the Worfklow Tips section of the manual answers "but why should I bother making that module"; basically putting the gist of that discourse thread in the documentation, where more people might see it.
1 parent f4dc5c1 commit 984485e

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

doc/src/manual/workflow-tips.md

+24-41
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,40 @@ your experience at the command line.
1010

1111
### A basic editor/REPL workflow
1212

13-
The most basic Julia workflows involve using a text editor in conjunction with the `julia` command
14-
line. A common pattern includes the following elements:
13+
The most basic Julia workflows involve using a text editor in conjunction with the `julia` command line.
1514

16-
* **Put code under development in a temporary module.** Create a file, say `Tmp.jl`, and include
17-
within it
15+
Create a file, say `Tmp.jl`, and include within it
16+
```julia
17+
module Tmp
1818

19-
```julia
20-
module Tmp
21-
export say_hello
19+
say_hello() = println("Hello!")
2220

23-
say_hello() = println("Hello!")
21+
# Your other definitions here
2422

25-
# your other definitions here
23+
end # module
2624

27-
end
28-
```
29-
* **Put your test code in another file.** Create another file, say `tst.jl`, which looks like
25+
using .Tmp
26+
```
27+
Then, in the same directory, start the Julia REPL (using the `julia` command).
28+
Run the new file as follows:
29+
```
30+
julia> include("Tmp.jl")
3031
31-
```julia
32-
include("Tmp.jl")
33-
import .Tmp
34-
# using .Tmp # we can use `using` to bring the exported symbols in `Tmp` into our namespace
32+
julia> Tmp.say_hello()
33+
Hello!
34+
```
35+
Explore ideas in the REPL. Save good ideas in `Tmp.jl`.
36+
To reload the file after it has been changed, just `include` it again.
3537

36-
Tmp.say_hello()
37-
# say_hello()
38+
The key in the above is that your code is encapsulated in a module.
39+
That allows you to edit `struct` definitions and remove methods, without restarting Julia.
3840

39-
# your other test code here
40-
```
41+
(Explanation: `struct`s cannot be edited after definition, nor can methods be deleted.
42+
But you _can_ overwrite the definition of a module, which is what we do when we re-`include("Tmp.jl")`).
4143

42-
and includes tests for the contents of `Tmp`.
43-
Alternatively, you can wrap the contents of your test file in a module, as
44+
In addition, the encapsulation of code in a module protects it from being influenced
45+
by previous state in the REPL, protecting you from hard-to-detect errors.
4446

45-
```julia
46-
module Tst
47-
include("Tmp.jl")
48-
import .Tmp
49-
#using .Tmp
50-
51-
Tmp.say_hello()
52-
# say_hello()
53-
54-
# your other test code here
55-
end
56-
```
57-
58-
The advantage is that your testing code is now contained in a module and does not use the global scope in `Main` for
59-
definitions, which is a bit more tidy.
60-
61-
* `include` the `tst.jl` file in the Julia REPL with `include("tst.jl")`.
62-
63-
* **Lather. Rinse. Repeat.** Explore ideas at the `julia` command prompt. Save good ideas in `tst.jl`. To execute `tst.jl` after it has been changed, just `include` it again.
6447

6548
## Browser-based workflow
6649

0 commit comments

Comments
 (0)