# Copying struct fields to another struct

**URL:** <https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 29, 2019, 6:00am UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493 "2019-03-29T06:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@aditya](https://discourse.julialang.org/u/aditya)\
**Post date:** [March 29, 2019, 6:00am UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493/1 "2019-03-29T06:00:25Z")

</div>

For example I have an -

abstract type A

abstract type B \<: A

struct C \<: B  
fields  
end

Now, I want another struct D which has all fields of C but is a subtype of A i.e,

struct D \<: A  
C fields  
end

Is there a way to do this, without manually copy pasting the contents ?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 29, 2019, 6:24am UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493/2 "2019-03-29T06:24:42Z")

</div>

No, at least not without macros. Copy and paste is honorable.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [March 29, 2019, 6:27am UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493/3 "2019-03-29T06:27:56Z")

</div>

Is [Mixers.jl](https://github.com/rafaqz/Mixers.jl) what you want? And what about [Classes.jl](https://github.com/rjplevin/Classes.jl)?

---

<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 20, 2020, 7:14pm UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493/4 "2020-07-20T19:14:42Z")

</div>

Mixers.jl will work, but it ends up constructing a whole bunch of macros. I managed to build a single macro that does what I need it to do, and handles parametric types.

```julia
function get_type_expression(X::Union{Symbol,DataType})
    if X isa Symbol
        return X
    elseif X isa DataType
        Params = X.parameters
        if isempty(Params)
            return Symbol(X.name.wrapper)
        else
            return Expr(:curly, Symbol(X.name.wrapper), Params...)
        end
    end
end

macro inherit_fields(AssetTypeExp::Expr)
    BaseAssetType = Core.eval(@ __MODULE__ , AssetTypeExp.args[1])
    TypeSymbols = AssetTypeExp.args[2:end]
    AssetType = BaseAssetType{TypeSymbols...}

    ex = Expr(:block)
    for fName in fieldnames(AssetType)
        fType = fieldtype(AssetType, fName)
        fTypeParsed = get_type_expression(fType)
        push!(ex.args, :( $fName :: $fTypeParsed ) )
    end

    return esc(ex)
end

```

You can then use this macro pretty intuitively as follows:

```julia
mutable struct MyOldType{T}
    Name :: Symbol
    A :: T
    B :: T
    V :: Vector{T}
end

mutable struct MyNewType{T}
    @inherit_fields(MyOldType{T})
    C :: T
end

```

As you can see, it’s all done in one neat step without building a whole bunch of specific macros as you would do in Mixers.jl. You can even modify this by adding a second argument that lists fields you want to leave out of the inheritance and filtering those out of the for loop (keep in mind that the field array will be an expression so you will have to evaluate it to get an array of Symbols).
