Hey! I have been trying to make a saving macro to simplify my life and skip having to do the full saving syntax while keeping track of what I’m saving where. To do this, I have started writing a module. The main macro is as follows:
macro saving(funcname::String,params...) quote oldfile($funcname) h5write($funcname*".h5","paramsNames",stringSymbolSeparator($params)) for (n,name) in enumerate(stringSymbolSeparator($params)) h5write($funcname*".h5",name,eval($(esc(params))[n])) println("Saved $($(params)[n])") end end end
Where the oldfile function just makes sure that there isn’t any file saved with funcname (and moves it to a .old file if it exists), and stringSymbolSeparator takes “params” as symbols and returns an array where each entry is the text used to write the variables (so that if I run
@saving "test" var1, it will save the variable var1 in “test.h5” HDF5 file under the name of “var1”). When I try running the macro with variables that I have saved in the global scope it seems to work fine, my problem is that if I run the macro inside of a function, the eval command seems to be evaluating within the wrong scope and I get an UndefVarError. I’m pretty sure that the workaround has something to do with the macro hygiene, but after looking for a while I just can’t seem to fix it.
Thanks a lot for your help