# What variable runtime values can be accessed in compile time (macro expansion)?

**URL:** https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136
**Category:** New to Julia
**Tags:** question, macros, metaprogramming
**Created:** [April 10, 2017, 1:30am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136 "2017-04-10T01:30:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)
#### Post date: [April 10, 2017, 1:30am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136/1 "2017-04-10T01:30:22Z")

</div>

As how I understand it, Julia parses and evaluates code piece by piece. Thus it is possible for a macro definition to access a variable’s runtime value when the macro is expanded. Whether or not it makes sense to read or write a variable’s value when a macro is expanded depends on the order in which the program is compiled. Does there exist such a specification about the ordering?

To give a few concrete examples of my confusion:

1. I think `const` global variables are bound to their value before any macro is expanded. Am I correct? I learned it from this question [Macro for counting the number of times a function is called?](https://discourse.julialang.org/t/macro-for-counting-the-number-of-times-a-function-is-called/3129) and how [Spark.jl](https://github.com/dfdx/Spark.jl/blob/master/src/attach.jl) defines `@attach` but I can’t yet find the documentation.

2. If I have some code like this:

```julia
a = some_function()
@some_macro d = 1 + 2

```

When `@some_macro d = 1 + 2` is expanded, can it reliably access `a`’s runtime value (the value returned by `some_function`?

1. If I import a module to my program as

```julia
import SomeModule
...other code...

```

can I be sure that `SomeModule`’s global variables are bound and macros are expanded before any macro in `...other code...` is expanded?

---

<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: [April 10, 2017, 1:55am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136/2 "2017-04-10T01:55:57Z")

</div>

> [@jinliangwei](#):
>
> As how I understand it, Julia parses and evaluates code piece by piece.

No. Only (kind of) for global scope. Not for local scope.

> [@jinliangwei](#):
>
> Thus it is possible for a macro definition to access a variable’s runtime value when the macro is expanded.

No.

> [@jinliangwei](#):
>
> Does there exist such a specification about the ordering?

Macro expands happens after parsing before any part of the code is run so it happens before everything else and has no access to runtime values of any local variables. Global statement are executed one by one so one global statement will be able to access any values from the previous statement. This includes macro expansion time since it also happens after the previous global statement is executed.

> [@jinliangwei](#):
>
> I think const global variables are bound to their value before any macro is expanded.

No.

> [@jinliangwei](#):
>
> When @some\_macro d = 1 + 2 is expanded, can it reliably access a’s runtime value (the value returned by some\_function?

No.

> [@jinliangwei](#):
>
> can I be sure that SomeModule’s global variables are bound and macros are expanded before any macro in …other code… is expanded?

Yes, because (as you write it) they are separate global statements.

---

<div class="post-metadata">

### Author: ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)
#### Post date: [April 10, 2017, 2:26am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136/3 "2017-04-10T02:26:58Z")

</div>

Thanks a lot for responding!

> Macro expands happens after parsing before any part of the code is run

> Global statement are executed one by one so one global statement will be able to access any values from the previous statement. This includes macro expansion time since it also happens after the previous global statement is executed.

Do not mean to get you caught up in technicality, but just to be clear 🙂 : macro expansion happens after it’s previous global statements are executed. So this is not before “any part of the code is run”. But if a macro is expanded within a local scope then it’s before any part of the local code is run.

```julia
a = some_function()
@some_macro d = 1 + 2

```

If this code is in global scope, then when `@some_macro d = 1 + 2` is expanded, it may access `a`’s value?

---

<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: [April 10, 2017, 2:39am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136/4 "2017-04-10T02:39:38Z")

</div>

> [@jinliangwei](#):
>
> So this is not before “any part of the code is run”

I did not say “any code”. “The code” here refer to the enclosing global statement.

> [@jinliangwei](#):
>
> If this code is in global scope, then when @some\_macro d = 1 + 2 is expanded, it may access a’s value?

Yes.

---

<div class="post-metadata">

### Author: ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)
#### Post date: [April 10, 2017, 5:08am UTC](https://discourse.julialang.org/t/what-variable-runtime-values-can-be-accessed-in-compile-time-macro-expansion/3136/5 "2017-04-10T05:08:34Z")

</div>

I see. Thanks!
