# Macro to create a struct with variable number of fields?

**URL:** https://discourse.julialang.org/t/macro-to-create-a-struct-with-variable-number-of-fields/24486
**Category:** New to Julia
**Tags:** macros
**Created:** [May 22, 2019, 4:23pm UTC](https://discourse.julialang.org/t/macro-to-create-a-struct-with-variable-number-of-fields/24486 "2019-05-22T16:23:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![FrankCimballi](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@FrankCimballi](https://discourse.julialang.org/u/FrankCimballi)
#### Post date: [May 22, 2019, 4:23pm UTC](https://discourse.julialang.org/t/macro-to-create-a-struct-with-variable-number-of-fields/24486/1 "2019-05-22T16:23:56Z")

</div>

My first failed attempt at a macro! I want to be able to generate a struct definition by passing a classname and fields. May code should look something like this?

```julia
macro create_class(classname, fields)
    quote
        mutable struct $classname
            $(fields...)
            function $classname($(fields...))
                new($(fields...))
            end
        end
    end
end

```

---

<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: [May 22, 2019, 4:27pm UTC](https://discourse.julialang.org/t/macro-to-create-a-struct-with-variable-number-of-fields/24486/2 "2019-05-22T16:27:44Z")

</div>

A macro is basically a function for syntax. So could you write down the input syntax and the desired output syntax?

Then you can run `dump` on the input and output syntax and figure out how to do the transformation.

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [May 23, 2019, 10:27pm UTC](https://discourse.julialang.org/t/macro-to-create-a-struct-with-variable-number-of-fields/24486/3 "2019-05-23T22:27:09Z")

</div>

You might be looking for something like this:

```julia
macro create_class(classname, fields...)
    quote
        mutable struct $classname
            $(fields...)
            function $classname($(fields...))
                new($(fields...))
            end
        end
    end
end

```

```julia
julia> @macroexpand @create_class foo bar baz
quote
    #= REPL[11]:3 =#
    mutable struct foo
        #= REPL[11]:4 =#
        bar
        baz
        #= REPL[11]:5 =#
        function foo(#3#bar, #4#baz)
            #= REPL[11]:6 =#
            new(#3#bar, #4#baz)
        end
    end
end

```

or this:

```julia
macro create_class2(classname, fields_tuple)
    fields = fields_tuple.args
    quote
        mutable struct $classname
            $(fields...)
            function $classname($(fields...))
                new($(fields...))
            end
        end
    end
end

```

```julia
julia> @macroexpand @create_class2 foo (bar, baz)
quote
    #= REPL[15]:4 =#
    mutable struct foo
        #= REPL[15]:5 =#
        bar
        baz
        #= REPL[15]:6 =#
        function foo(#7#bar, #8#baz)
            #= REPL[15]:7 =#
            new(#7#bar, #8#baz)
        end
    end
end

```

In any case, I would recommend following kristoffer’s advice, especially the part about `dump()`ing the macro arguments since I think that was what caused you problems here.
