# Add lines of code in a struct definition

**URL:** https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297
**Category:** General Usage
**Created:** [July 18, 2020, 5:40pm UTC](https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297 "2020-07-18T17:40:50Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![Deduction42](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deduction42/32/9206_2.png) [@Deduction42](https://discourse.julialang.org/u/Deduction42)
#### Post date: [July 19, 2020, 2:39pm UTC](https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297/7 "2020-07-19T14:39:57Z")

</div>

Actually, I ended up seeing something similar to that which helped me. Basically the macro that inserted the lines used the form

```julia
macro add_some_fields()
    return esc(:(A::Int64; B::Float64))
end

```

Which worked as expected. The only problem is that my macro produced the fields and the types by looking at a parent object that was an input. Now let us say I wanted to create a pump type CentrifugalPump\_Type and inherit all the fields from GenericPump\_Type some exceptions (such as specifications). I would have to generate a string representing that code and then parse it. When I used the following command:

```julia
InnerExp = :(
    A::Int64;
    B::Float64; )

```

I would get

```julia
quote
    A::Int64
    #= none:1 =#
    B::Float64
end

```

I tried using

```julia
CodeLines = "A::Int64; B::Float64"
InnerExp = Meta.parse("quote $CodeLines end")
>>
:($(Expr(:quote, quote
    #= none:1 =#
    A::Int64
    #= none:1 =#
    B::Float64
end)))

```

This was not the result I expected, but buried somewhere in the internet, I found someone suggesting a begin … end statement. So I tried this

```julia
InnerExp = Meta.parse("begin $CodeLines end")
quote
    #= none:1 =#
    A::Int64
    #= none:1 =#
    B::Float64
end

```

Which yielded the same result as ` :( A::Int64; B::Float64; )`. So I guess putting multiple lines in an expression is implicitly assuming a “begin…end” statement. This is what ended up working in the simple macro.

```julia
macro add_some_fields()
    CodeLines = "A::Int64; B::Float64"
    return esc(Meta.parse("begin $CodeLines end"))
end

```

Now that CodeLines are strings, I can very easily generate any kind of code that I want.

---

_[View the full topic](https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297)._
