I have some helper functions in a separate script that I `source` from other scripts. It may happen, especially when working interactively, that I source/define the function even though it already exists. I'd like to add a line like mata: capture drop function_name() ahead of the function definition, but there is no capturing in Mata.
Other ideas besides capture:
This fails when the function does not exist.
This fails because Mata/Stata is declarative and doesn't like contingent drops ... or something.
This fails because Mata/Stata is declarative and doesn't like contingent definitions ... or something.
I got the idea for these latter two from another thread. A 2008 thread seems very closely related, but I can't figure out what is going on in it.
Code:
mata: function f(){ return(1) } end ** do some stuff ** remove function or maybe don't ** later, want to make sure the function is available <<< What can I insert here? >>> mata: function f(){ return(1) } end
Code:
mata: mata drop f()
Code:
mata: if (findexternal("f()") != NULL){ mata drop f() } end mata: function f(){ return(1) } end
Code:
mata: if (findexternal("f()") == NULL){ function f(){ return(1) } } end
I got the idea for these latter two from another thread. A 2008 thread seems very closely related, but I can't figure out what is going on in it.
Comment