# An confused error for the Marco for Expr to function

**URL:** https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485
**Category:** General Usage
**Created:** [May 30, 2020, 9:56pm UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485 "2020-05-30T21:56:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [May 30, 2020, 9:56pm UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485/1 "2020-05-30T21:56:32Z")

</div>

I try to use the macro

```julia
macro expr2fn(fname, expr, args...)
    fn = quote
        function $(esc(fname))()
            $(esc(expr.args[1]))
        end
    end
    for arg in args
        push!(fn.args[2].args[1].args, esc(arg))
    end
    return fn
end

```

Then I use :  
@expr2fn(f, :((n1+n2+n3)), n1, n2, n3 )  
f(0.1,0.2,0.3)  
This is fine.

When I use like :  
expr2=:((n1+n2+n3))  
@expr2fn(test2\_func, expr2, n1, n2, n3 )

It generate an error

> Blockquote  
> LoadError: type Symbol has no field args  
> in expression starting at /Users/gangchen/Downloads/sample.jl:92  
> getproperty(::Symbol, ::Symbol) at sys.dylib:?  
> @expr2fn(::LineNumberNode, ::Module, ::Any, ::Any, ::Vararg{Any,N} where N) at sample.jl:65  
> #macroexpand#32 at expr.jl:107 [inlined]  
> (::Base.var"#kw##macroexpand")(::NamedTuple{(:recursive,),Tuple{Bool}}, ::typeof(macroexpand), ::Module, ::Expr) at none:0  
> top-level scope at sample.jl:92  
> include\_string(::Module, ::String, ::String) at sys.dylib:?  
> include\_string(::Module, ::String, ::String, ::Int64) at eval.jl:30  
> (::Atom.var"#188#192"{String,Int64,String,Bool})() at eval.jl:111  
> withpath(::Atom.var"#188#192"{String,Int64,String,Bool}, ::String) at utils.jl:30  
> withpath(::Function, ::String) at eval.jl:9  
> #187 at eval.jl:110 [inlined]  
> with\_logstate(::Atom.var"#187#191"{String,Int64,String,Bool}, ::Base.CoreLogging.LogState) at logging.jl:395  
> with\_logger at logging.jl:491 [inlined]  
> #186 at eval.jl:109 [inlined]  
> hideprompt(::Atom.var"#186#190"{String,Int64,String,Bool}) at repl.jl:140  
> macro expansion at eval.jl:108 [inlined]  
> macro expansion at dynamic.jl:24 [inlined]  
> eval(::String, ::Int64, ::String, ::String, ::Bool) at eval.jl:105  
> macro expansion at eval.jl:39 [inlined]  
> (::Atom.var"#172#173")() at task.jl:333

Any one know what happens and how to fix the problem?

---

<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: [May 30, 2020, 10:13pm UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485/2 "2020-05-30T22:13:56Z")

</div>

It is impossible for macro to read from a runtime variable. If the value is know before runtime, just use the first one.

Even better, remove the quote from that argument.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 30, 2020, 10:16pm UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485/3 "2020-05-30T22:16:41Z")

</div>

Standard question: What are you trying to accomplish using a macro here? Are you passing expressions as arguments and using metaprogramming when you could pass function arguments instead?

---

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [May 31, 2020, 4:31am UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485/4 "2020-05-31T04:31:51Z")

</div>

Yes. An additional question, are there any method can transform Julia expr to a function? We hope it can be used in REPL and in functions.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 31, 2020, 3:07pm UTC](https://discourse.julialang.org/t/an-confused-error-for-the-marco-for-expr-to-function/40485/5 "2020-05-31T15:07:55Z")

</div>

> [@gangchern](#):
>
> > [@stevengj](#):
> >
> > Are you passing expressions as arguments and using metaprogramming when you could pass function arguments instead?
> 
> Yes.

Assuming your “yes” referred to my question: don’t use expressions to pass functions. Use functions to pass functions.

> [@gangchern](#):
>
> We hope it can be used in REPL and in functions.

What are you trying to accomplish? I think we may have an [XY problem](https://en.wikipedia.org/wiki/XY_problem) here.
