# Generating for loops with macros

**URL:** <https://discourse.julialang.org/t/generating-for-loops-with-macros/4654>\
**Category:** General Usage\
**Tags:** question, macros\
**Created:** [July 5, 2017, 3:26am UTC](https://discourse.julialang.org/t/generating-for-loops-with-macros/4654 "2017-07-05T03:26:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 5, 2017, 3:26am UTC](https://discourse.julialang.org/t/generating-for-loops-with-macros/4654/1 "2017-07-05T03:26:06Z")

</div>

I am having difficulty trying to write a macro that takes a vector of types, defines 3 input parameters for the constructor of each type, loops over different values of the parameters and instantiates the types inside of the loop. Consider this pseudo-code:

```julia
using Plots, Interact

macro loop_ctor(types)
  quote
    for i=1:length($types)
      # define variables ai, bi, ci for the ith type
    end
    # write a for loop with all variables defined above
    @manipulate for a1=1:10, b1=1:10, c1=1:10, a2=1:10, b2=1:10, c2=1:10, ...
      # instantiate the types
      obj1 = types[1](a1, b1, c1)
      obj2 = types[2](a2, b2, c2)
      ...

      # compose with some container type
      objs = MyType(obj1, obj2, ...)

      # plot with Plots.jl
      plot(objs)
    end
    end
  end
end

```

I am very new to macros and couldn’t progress much after reading the documentation online. Could someone give me a hand?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [July 5, 2017, 4:03am UTC](https://discourse.julialang.org/t/generating-for-loops-with-macros/4654/2 "2017-07-05T04:03:09Z")

</div>

It sounds like you may be reaching for the wrong tool. “A vector of types” sounds like something that needs a function, and doesn’t make any sense from a macro.

```julia
function loop_ctor(types)
    As = [#= define something =#(typ) for type in types]
    # write a for loop with all variables defined above
    @manipulate for I in Iterators.product(As...)
      # instantiate the types
      objs = [typ(I...) for typ in types]

      # compose with some container type
      container = MyType(objs)

      # plot with Plots.jl
      plot(container)
    end
end

```
