Rethink noclearall and onlyclearglobals options of the dynare command
Historically, Dynare was issuing a clear all
command at startup, to start from a fresh state and avoid problems related to data persistence (global and persistent variables, generated functions and MEX files).
However this behaviour was making Dynare hard to use in the context of repeated executions, hence the noclearall
option was introduced to suppress clear all
.
But noclearall
was not very useful in many contexts where the model would change too much between runs (because of data persistence issues), hence a new onlyclearglobals
option was requested (#635 (closed)) then implemented (preprocessor@0f24ec02). The idea was to clear only the globals used by Dynare and not other globals (but it does not clear persistent variables). Incidentally, the implementation was buggy (fixed in preprocessor@c1381f1d).
Later, in 2015, the default behaviour of Dynare has been changed: it no longer issues a clear all
; it now issues a clearvars -global
(to clear all global variables) and clears persistent variables (through a rather ad hoc mechanism in matlab/clear_persistent_variables.m
). This was implemented in preprocessor@4537f466, and the noclearall
option now disables this default behaviour.
It seems to me that the current situation is both messy and suboptimal.
To summarize the current situation:
- the default behaviour is to clear all global variables and only the relevant persistent variables
- the
onlyclearglobals
clears only the relevant global variables, but not persistent variables
It seems to me that the default should be to clear only the relevant global and persistent variables (hence a combination of the two above behaviours).
Then the noclearall
and onlyclearglobals
should probably merged in a single new option to disable this clearing, or maybe even dropped (because the behaviour of Dynare is undefined when this clearing does not occur).