# Pass variable value to macro inside a for loop

**URL:** https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879
**Category:** General Usage
**Created:** [March 26, 2017, 12:40am UTC](https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879 "2017-03-26T00:40:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [March 26, 2017, 12:40am UTC](https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879/1 "2017-03-26T00:40:37Z")

</div>

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?

* * *

```julia
  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

```julia
ERROR: LoadError: UndefVarError: loaded_folder not defined

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 26, 2017, 12:51am UTC](https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879/2 "2017-03-26T00:51:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [March 26, 2017, 1:04am UTC](https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879/3 "2017-03-26T01:04:22Z")

</div>

> [@yuyichao](#):
>
> use eval to splice in the value and expand the macro at runtime

What do you mean by this?

That seems like the most matter-of-fact answer

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 26, 2017, 1:19am UTC](https://discourse.julialang.org/t/pass-variable-value-to-macro-inside-a-for-loop/2879/4 "2017-03-26T01:19:11Z")

</div>

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