# How to assign value in macro from a function call

**URL:** <https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 21, 2018, 3:06pm UTC](https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283 "2018-09-21T15:06:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [September 21, 2018, 3:06pm UTC](https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283/1 "2018-09-21T15:06:13Z")

</div>

I tried to follow this [example](https://discourse.julialang.org/t/how-to-use-destructuring-assignment-with-macro/7385) to build a macro that does such thing: `x = f(args...)`:

```julia
julia> function mysum(args...)
             sum([x for x in args])
             end
mysum (generic function with 1 method)

julia> macro myassign(sym, f, args...)
           @show sym
           @show f
           @show args

           return esc(Expr(:(=), esc(sym), :($f($(esc(Expr(:tuple, args...)))...))))
       end
@myassign (macro with 1 method)

julia> @myassign(:x, f, 1,2,3,4)
sym = :(:x)
f = :f
args = (1, 2, 3, 4)
ERROR: syntax: invalid assignment location "(escape (inert x))"

```

I could let the macro return the function evaluation results but not for the assignment. I am completely lost in this. Could you please give me some clue to follow? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 21, 2018, 3:12pm UTC](https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283/2 "2018-09-21T15:12:18Z")

</div>

Perhaps this is an easier way to write it:

```julia
julia> macro myassign(sym, f, args...)
           return :($(esc(sym)) = $(esc(f))($(esc.(args)...)))
       end
@myassign (macro with 1 method)

julia> @macroexpand @myassign(x, f, 1,2,3,4)
:(x = f(1, 2, 3, 4))

```

---

<div class="post-metadata">

**Author:** ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)\
**Post date:** [September 21, 2018, 7:25pm UTC](https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283/3 "2018-09-21T19:25:00Z")

</div>

Thank you! Your solution clear my doubts and mess in my code.

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [September 21, 2018, 10:52pm UTC](https://discourse.julialang.org/t/how-to-assign-value-in-macro-from-a-function-call/15283/4 "2018-09-21T22:52:53Z")

</div>

Or with a single escape

```julia
macro myassign(sym, f, args...)
    :($sym = $f($(args...))) |> esc
end

```
