You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardexpand all lines: doc/src/manual/workflow-tips.md
+24-41
Original file line number
Diff line number
Diff line change
@@ -10,57 +10,40 @@ your experience at the command line.
10
10
11
11
### A basic editor/REPL workflow
12
12
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.
15
14
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
18
18
19
-
```julia
20
-
module Tmp
21
-
export say_hello
19
+
say_hello() =println("Hello!")
22
20
23
-
say_hello() =println("Hello!")
21
+
# Your other definitions here
24
22
25
-
# your other definitions here
23
+
end# module
26
24
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")
30
31
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.
35
37
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.
38
40
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")`).
41
43
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.
44
46
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.
0 commit comments