# Why is "macro" different from "function" if everything is an expression?

**URL:** <https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043>\
**Category:** Internals & Design\
**Created:** [August 16, 2020, 1:01pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043 "2020-08-16T13:01:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [August 16, 2020, 1:01pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043/1 "2020-08-16T13:01:11Z")

</div>

I’m learning the basics of macros. Coming from another LISP-inspired language (Wolfram Mathematica), I’m wondering why there is a distinction between macros and functions. Isn’t a macro just a function whose argument is an expression? Is it correct to say that the “macro” keyword is just syntax sugar which allows you to write  
@some\_marco expression  
instead of  
some\_function(:(expression)) ?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 16, 2020, 1:22pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043/2 "2020-08-16T13:22:24Z")

</div>

Welcome to Julia-discourse!  
No, the difference is when the function/macro is evaluated. The latter is done at parse-time, i.e. before the code is run. Watch [Jacob Quinn: What happens when - From parse time to compile time - YouTube](https://youtu.be/RYZkHudRTvI)

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 16, 2020, 1:42pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043/3 "2020-08-16T13:42:32Z")

</div>

You can actually call it as a function. Try `var"@some_macro"(LineNumberNode(@ __LINE__ , @ __FILE__ ), @ __MODULE__ , :(expression))` instead, e.g.

```julia
julia> var"@show"(LineNumberNode(@ __LINE__ , @ __FILE__ ), @ __MODULE__ , :(1 + 2))
quote
    println("1 + 2 = ", repr(begin
                #= show.jl:641 =#
                value = $(Expr(:escape, :(1 + 2)))
            end))
    value
end

```

The `var"..."` around the macro name is just so that the `@` in front of the name gets interpreted as just a name, not as an actual macro call. The `LineNumberNode` and module tell the macro where it is expanded in, which can be useful for debugging and a lot of other things.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [August 16, 2020, 1:47pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043/4 "2020-08-16T13:47:06Z")

</div>

Thanks for the explanation. If you don’t care about performance penalties, is it OK to use a function to manipulate the syntax tree of an expression and then hit the modified expression with “eval”? For example, here’s a function which changes any integer assignment like “a=100” into a float assignment like “a=100.0”.

```
function change(expr::Expr)
    if expr.head == :(=)
        expr.args[2] = float(expr.args[2])
        eval(expr)
        return 0
    else
        return -1
    end
end

```

Then I can call the function with  
change(:(a=100))

In the end, the value of “a” will be the floating-point number 100.0.

---

<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:** [August 16, 2020, 2:05pm UTC](https://discourse.julialang.org/t/why-is-macro-different-from-function-if-everything-is-an-expression/45043/5 "2020-08-16T14:05:00Z")

</div>

No it’s not. They have completely different scope. There’s no way to emulate macros with `eval`. `eval` only runs in global scope and have no access to any local variables that’s accessible by code returned from macros.
