# Macro magic: looping over varargs printing values and symbols

**URL:** <https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [April 3, 2017, 4:47pm UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025 "2017-04-03T16:47:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kkmann/32/1727_2.png) [@kkmann](https://discourse.julialang.org/u/kkmann)\
**Post date:** [April 3, 2017, 4:47pm UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025/1 "2017-04-03T16:47:55Z")

</div>

Hey,

I am embarrassed to admit that today I surrendered to Julia’s meta programming x)

What I want to do is write a macro @check a b c … which checks the varargs input in turn and prints an informative error message with both name (symbol) and value of the item that caused the trouble. So, If I want to print the variables name  
as well, I guess I’ll need a macro?

```julia
macro check(x)
    name = string(x)
    :($x < 0 ? error(@sprintf("%s must be >= 0, is %.5f", $name, $x)) : true)
end

```

works fine for one input but I simply cannot get it to work with multiple inputs. Any attempt to loop over an input tuple x…  
fails, e.g.

```julia
macro checks(x...)
    tmp = Expr(:block)
    for xx in x
        name = string(xx)
        append!(tmp.args, :($xx < 0 ? error(@sprintf("%s must be >= 0, is %.5f", $name, $xx)) : true))
    end
    return(tmp)
end

```

fails with: no method matching length(:Expr) .

Best,

Kevin

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 3, 2017, 5:32pm UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025/2 "2017-04-03T17:32:24Z")

</div>

`append!` appends a _list_ to an existing list, not just a single element. You want `push!`. (this is a common pitfall when switching from Python 😉 ).

---

<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 3, 2017, 5:44pm UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025/3 "2017-04-03T17:44:35Z")

</div>

Also note that you should esc x.

---

<div class="post-metadata">

**Author:** ![kkmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kkmann/32/1727_2.png) [@kkmann](https://discourse.julialang.org/u/kkmann)\
**Post date:** [April 4, 2017, 7:45am UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025/4 "2017-04-04T07:45:49Z")

</div>

@rdeits thanks, that did the job - sorry for blaming macro magic 🙏

@yuyichao, nope, escaping x does not work - I think its because I did not quote the macro body which would also be an option (I guess)

So for completeness’ sake:

```julia
macro checks(x...)
    tmp = Expr(:block)
    for xx in x
        name = string(xx)
        push!(tmp.args, :($xx < 0 ? error(@sprintf("%s must be >= 0, is %.5f", $name, $xx)) : true))    
    end
    return(tmp)
end

```

works for me, thanks again!

---

<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 4, 2017, 11:16am UTC](https://discourse.julialang.org/t/macro-magic-looping-over-varargs-printing-values-and-symbols/3025/5 "2017-04-04T11:16:06Z")

</div>

> [@kkmann](#):
>
> nope, escaping x does not work

I don’t know what do you mean by “not work” but anyway, expect this to break on 0.6.
