# How to simplify the code in constructing struct

**URL:** https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411
**Category:** New to Julia
**Tags:** inheritance, struct
**Created:** [October 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411 "2021-10-26T15:07:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![zhangjin](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhangjin](https://discourse.julialang.org/u/zhangjin)
#### Post date: [October 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/1 "2021-10-26T15:07:39Z")

</div>

I know the julia don’t have the “class”, but I need it. So I try to use struct to complete it. I try to implement the initialization of inheritance like following code.

```julia
struct P1
    M::Int8
    Ks::Int8
    metric::String
end

struct P2
    D::Int16
    Ds::Int16 
    k_v::Int64
    function P2(D,k_v)
        Ds = D*k_v
        new(D, Ds, k_v)
    end
end

struct C
    M::Int8
    Ks::Int8
    metric::String

    D::Int16
    Ds::Int16 
    k_v::Int64

    function C(M, Ks, metric, D, k_v)
        p1 = P1(M, Ks, metric)
        M, Ks, metric = p1.M, p1.Ks, p1.metric
        
        p2 = P2(D, k_v)
        D, Ds, k_v = p2.D, p2.Ds, p2.k_v 

        new(M, Ks, metric, D, Ds, k_v)
    end 
end

```

But I notice when the field of struct change, it is troublesome to adjust the code. I want to know the more reasonable ways to implement it.

Thanks in advance for any help/suggestion.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 26, 2021, 3:12pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/2 "2021-10-26T15:12:03Z")

</div>

Why not just

```julia

struct C
    p1 :: P1
    p2 :: P2

    function C(M, Ks, metric, D, k_v)
        p1 = P1(M, Ks, metric)
        p2 = P2(D, k_v)
        new(p1, p2)
    end 
end

```

---

<div class="post-metadata">

### Author: ![zhangjin](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhangjin](https://discourse.julialang.org/u/zhangjin)
#### Post date: [October 26, 2021, 3:17pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/3 "2021-10-26T15:17:24Z")

</div>

@Oscar_Smith It is ok. If I can access the field without the name of “parents”, I think it is better.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [October 26, 2021, 3:34pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/4 "2021-10-26T15:34:32Z")

</div>

See [this](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/5) thread for related discussion. If you’re worried about the visual noise that results from accessing deeply nested fields, you may appreciate [UnPack.jl](https://github.com/mauro3/UnPack.jl).

---

<div class="post-metadata">

### Author: ![zhangjin](https://avatars.discourse-cdn.com/v4/letter/z/a9a28c/32.png) [@zhangjin](https://discourse.julialang.org/u/zhangjin)
#### Post date: [October 26, 2021, 3:46pm UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/5 "2021-10-26T15:46:17Z")

</div>

@stillyslalom Thanks for your answer, It is very useful.

---

<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: [October 31, 2021, 3:46am UTC](https://discourse.julialang.org/t/how-to-simplify-the-code-in-constructing-struct/70411/6 "2021-10-31T03:46:09Z")

</div>

You might want to have a look at these packages:

- [YingboMa/Unityper.jl (github.com)](https://github.com/YingboMa/Unityper.jl)
- [rjplevin/Classes.jl: A simple, Julian approach to inheritance of structure and methods (github.com)](https://github.com/rjplevin/Classes.jl)
- [gcalderone/ReusePatterns.jl: Implement composition and concrete subtyping in Julia. (github.com)](https://github.com/gcalderone/ReusePatterns.jl)
- [ANN: StructuralInheritance.jl - Community - JuliaLang](https://discourse.julialang.org/t/ann-structuralinheritance-jl/16515)
- [rafaqz/Mixers.jl: Julia mixin macros. Mixed, not stirred (github.com)](https://github.com/rafaqz/Mixers.jl)
- [JeffreySarnoff/TypedDelegation.jl: Easily apply functions onto fields’ values. Use a struct’s fields as operands for operations on values of that type. (github.com)](https://github.com/JeffreySarnoff/TypedDelegation.jl)
