Skip to content

Commit

Permalink
manual/workflow-tips: simplify the basic workflow (and explain the wh…
Browse files Browse the repository at this point in the history
…y) (#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.
  • Loading branch information
tfiers authored and pull[bot] committed Nov 2, 2023
1 parent f4dc5c1 commit 984485e
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions doc/src/manual/workflow-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,40 @@ your experience at the command line.

### A basic editor/REPL workflow

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

* **Put code under development in a temporary module.** Create a file, say `Tmp.jl`, and include
within it
Create a file, say `Tmp.jl`, and include within it
```julia
module Tmp

```julia
module Tmp
export say_hello
say_hello() = println("Hello!")

say_hello() = println("Hello!")
# Your other definitions here

# your other definitions here
end # module

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

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

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

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

```julia
module Tst
include("Tmp.jl")
import .Tmp
#using .Tmp
Tmp.say_hello()
# say_hello()
# your other test code here
end
```

The advantage is that your testing code is now contained in a module and does not use the global scope in `Main` for
definitions, which is a bit more tidy.

* `include` the `tst.jl` file in the Julia REPL with `include("tst.jl")`.

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

## Browser-based workflow

Expand Down

0 comments on commit 984485e

Please sign in to comment.