# Can't create a struct with a macro from a string of types

**URL:** <https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876>\
**Category:** General Usage\
**Created:** [May 26, 2021, 5:58pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876 "2021-05-26T17:58:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![emanuellima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emanuellima/32/25505_2.png) [@emanuellima](https://discourse.julialang.org/u/emanuellima)\
**Post date:** [May 26, 2021, 5:58pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876/1 "2021-05-26T17:58:17Z")

</div>

Hello. I am trying to create a struct through a macro with types that will be know at runtime. It would work something like this: the user would pass a string “a::A b::B” and the macro would create a struct:

struct MyStruct  
a::A  
b::B  
end

The problem is I don’t know how to break the types from the string to create the struct in the macro. Any help would be appreciated.

---

<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:** [May 26, 2021, 6:07pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876/2 "2021-05-26T18:07:11Z")

</div>

I’d suggest avoiding strings entirely. There’s no need to manually construct or parse strings for metaprogramming in Julia, since we have much better tools for working directly with expressions.

For example, you could do something like this:

```julia
julia> macro make_struct(fields...)
         quote
           struct MyStruct
             $(esc.(fields)...)
           end
         end
       end
@make_struct (macro with 2 methods)

julia> @make_struct(x::Int, y::Float64)

```

We can use `@macroexpand` to verify the macro’s behavior:

```julia
julia> @macroexpand @make_struct(x::Int, y::Float64)
quote
    #= REPL[7]:3 =#
    struct MyStruct
        #= REPL[7]:4 =#
        x::Int
        y::Float64
    end
end

```

The `@make_struct` macro takes the field definitions as _expressions_, so there’s no need to try to split up the string or extract the types because the Julia parser has already done that for you.

---

<div class="post-metadata">

**Author:** ![emanuellima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emanuellima/32/25505_2.png) [@emanuellima](https://discourse.julialang.org/u/emanuellima)\
**Post date:** [May 26, 2021, 6:09pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876/3 "2021-05-26T18:09:15Z")

</div>

Right, but the problem is, the types will come from a TOML file, and so will live as a string inside the Julia program.

---

<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:** [May 26, 2021, 6:13pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876/4 "2021-05-26T18:13:41Z")

</div>

Ok, then I’d suggest:

- Use `Meta.parse` to turn a string into an expression
- Don’t use a macro at all–just write a function that calls `eval`, something like:

```julia
julia> function make_struct(fields)
           eval(quote
             struct MyStruct
               $(fields...)
             end
           end)
       end

```

where `fields` is a collection of `Expr`s from `Meta.parse`

---

<div class="post-metadata">

**Author:** ![emanuellima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emanuellima/32/25505_2.png) [@emanuellima](https://discourse.julialang.org/u/emanuellima)\
**Post date:** [May 26, 2021, 6:15pm UTC](https://discourse.julialang.org/t/cant-create-a-struct-with-a-macro-from-a-string-of-types/61876/5 "2021-05-26T18:15:04Z")

</div>

Alright, I will try that. Thanks!
