I am used to starting all my Matlab scripts with clear all; close all; clc to ensure I am not looking at any old data or plots. I found Julia’s clearconsole() to be equivalent to Matlab’s clc, but don’t have working solutions for the other two Matlab commands yet. I mostly work in the Juno IDE and run scripts with the Play (“Run All”) button.
I understand that the Revise.jl package is supposed to help refresh the workspace now that workspace() is deprecated, but it doesn’t work for this simple test case. If I define x once and then comment that line out, it will continue to print each time I run without error.
using Revise
clearconsole()
#x=1
println(x)
I know I can hit “Stop” then “Play” to reset the workspace. However, that still doesn’t close old plots, and the time to first plot problem makes this option undesirable.
I found the “Forget All Plots” button in Juno’s plot pane, but I would like to have that functionality as a line in my script instead. Currently, it takes me three clicks to run a script again after I edit it (four if I include “Stop”).
“Forget All Plots”
Somewhere in the editor to put focus back on my current file.
“Run All”
I would ideally like to re-run in a fresh environment with one click or keystroke, but any tips on a better Juno workflow would be appreciated.
For a “clear all” equivalent, you can use a module. If your script looks like this:
module MyModule
x = 3
end #module
Then, each time you re-run the script, you will see the message “WARNING: replacing module MyModule”, and any functions/variables that you created in there will be lost.
At the REPL, you can access the variables as e.g. MyModule.x.
If you want to compare the results before/after some change to the code, then re-name the module in your script and run it again. Now you have access to results of both runs, and can compute things like MyModule.x - MyNewModule.x.
Another option is to write functions instead of scripts.
This is a cool option that I will definitely use for testing script changes, but wrapping code in a module or function makes the Juno Workspace pretty useless for debugging at a glance. On the other hand, I know I am supposed to avoid global variables in Julia, so maybe I will need to come up with another method for rapid debugging. What is typically done to glance at all the variables inside a function and check that they have the correct type and value?
I try to wrap all my work in functions so that I am mostly dealing with local variables rather than lingering globals (unless I am doing very quick prototyping in the REPL). This also has the added benefit of faster performance but ofcourse, when prototyping or quick data analysis, optimization/speed isn’t a big concern.