I want to be able to Get a .wl file. Run some functions (who will do their job working on some separate files that are being written away). And then return to some clean slate so that I can repeat this process with another .wl file and some other functions. This is close to using Quit between these two actions but I want
- To keep some basic definitions or at worst automatically redefine them after we obtained a new empty state.
- To be able to have multiple of these inputs in one notebook and have it be runable as a whole.
Quit would fail at both these things. My solution for now is to define the basic definitions, protect them and then use functions such as
run[wlfile] := Module[{},
Quiet@ClearAll["Global`*"];
Get[(fileName[wlfile])];
ComputationsUsingDefinitionsInWLFile[a][b][c];
Quiet@ClearAll["Global`*"];
]
Here a
,b
,c
would be set globally and protected as would fileName[file_]
(defined as a function that points to the correct file) and run
.
Is there some scoping construct that would do a better job at this?
(I don't know the exact symbols that will be affected by the .wl file (and there will be many) so I can't specifically Block them.)
Ideally the solutions can be used in the following form
throwAwayEnvironment[codeToRun,getCode]
where the first argument contains the code to be run after performing the getCode
which Gets definitions (which could be necessary for codeToRun
). The answer by Carl seemed to be able to be written in this form, but I ran into trouble due to the fact that any function called inside Internal ` WithLocalSettings
got saved to the wrong environment and did not get removed afterwards. First calling all functions as Symbol["f"]
inside WithLocalSettings
solves this. However, I then need to automate the finding of functions inside codeToRun
and run Symbol["function"] for all of them and all of that without the function first being saved as Global
variables. This last seems to be impossible since this is done instantly. Note that if you use Symbol[ToString[f]]
, this already no longer works since f
will already have been made into a global
variable.