Pass variable value to macro inside a for loop


#1

I’m trying to get the following lines of code to work, but @export_all_files keeps getting a symbol with:

  • the value loaded_folder
  • instead of the folder it should be loaded (i.e. src or config/initializers)

Why is that happening?


  loaded_folders = [ "src", "config/initializers" ]

  for loaded_folder in loaded_folders
    loaded_folder = "$(pwd())/$loaded_folder"

    Julz.include_all_files(loaded_folder)
    Julz.@export_all_files loaded_folder
  end

yields the error

ERROR: LoadError: UndefVarError: loaded_folder not defined

#2

Macro expansion happens at parse time so no it’s impossible to get the value of loaded_folder in the macro. You can either use a function, or expand to a piece of code that uses it at runtime (possibly by calling functions), or use eval to splice in the value and expand the macro at runtime.


#3

What do you mean by this?

That seems like the most matter-of-fact answer


#4

@eval Julz.@export_all_files $loaded_folder or eval(:(Julz.@export_all_files $loaded_folder)). Obviously only works in global scope.