# Generate a (mutable) struct which inherits others

**URL:** <https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347>\
**Category:** New to Julia\
**Tags:** inheritance, struct\
**Created:** [September 27, 2020, 2:59am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347 "2020-09-27T02:59:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 27, 2020, 2:59am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/1 "2020-09-27T02:59:15Z")

</div>

Hi.

Is there any way for a (mutable) struct to inherit fields from others?

For example, what I’m trying to do is:

```julia
mutable struct Parent
    foo
    bar
end

mutable struct Child <: Parent
end

```

and use `child = Child(); child.foo = 1` (struct field inheritance).  
However, an error was raised from the above code: `invalid subtyping in definition of Child`.

Is there something I missed?

---

<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:** [September 27, 2020, 3:12am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/2 "2020-09-27T03:12:15Z")

</div>

Nope, Julia doesn’t let you inherit from concrete types. Only abstract types can have subtypes. That said, usually what you want to do here is just have

```julia
mutable struct Child
    x::Parent
end

```

and then define methods appropriately.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 27, 2020, 3:15am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/3 "2020-09-27T03:15:26Z")

</div>

Go allows direct access to "sub-types " if names don’t clash.

But you can achieve something similar by overload the `getproperty` method

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 27, 2020, 3:33am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/4 "2020-09-27T03:33:15Z")

</div>

@xiaodai @Oscar_Smith  
Can you give me a more specific example to inherit “fields” from other struct?  
Or, is it impossible in Julia? Should I write all individual fields manually?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 27, 2020, 3:43am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/5 "2020-09-27T03:43:58Z")

</div>

Follow this example and just work thru it

```julia
mutable struct Parent
    foo
    bar
end

mutable struct Child
    parent::Parent
    foo # this is duplicated with parent
    notbar
end

parent = Parent(1, 2)

child = Child(parent, 3, 4)

child.bar # doesn't work

function Base.getproperty(child::Child, prop::Symbol)
    if prop in fieldnames(Parent) && !(prop in fieldnames(Child))
        return getfield(child.parent, prop)
    else
        return getfield(child, prop)
    end
end

child.bar # works now

child.foo # returns child value instead of parent value;

child.parent.foo # return parent's foo

```

This shows the underlying mechanism. If you do this pattern alot you can make it easier by using a macro to write those codes for you.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 27, 2020, 3:46am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/6 "2020-09-27T03:46:45Z")

</div>

Thanks a lot.

It seems useful, but I don’t know how to make a macro.  
I would have to look at macro 🙂 thx.

+) There’s no famous packages which offers such macros in Julia community? @xiaodai

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 27, 2020, 3:50am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/7 "2020-09-27T03:50:06Z")

</div>

> [@iHany](#):
>
> +) There’s no famous packages which offers such macros in Julia community?

maybe i am not sure. maybe someone will point the way. But this pattern is not that common in Julia I would guess. You can acheive what u want (what u really want functionally, not aesthetically) using more Julian ways I would guess.

Also I suspect [Hands-On Design Patterns and Best Practices with Julia](https://subscription.packtpub.com/book/programming/9781838648817/8/ch08lvl1sec38/the-delegation-pattern)

talks about it but you have to buy the book to support Tom first. 🙂

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [September 27, 2020, 3:54am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/8 "2020-09-27T03:54:30Z")

</div>

You might find this discussion useful if you haven’t already seen it: [Composition and inheritance: the Julian way](https://discourse.julialang.org/t/composition-and-inheritance-the-julian-way/11231)

This package [https://github.com/rjplevin/Classes.jl](https://github.com/rjplevin/Classes.jl) can help emulate something like what you’re asking for, but I would keep in mind that this is not usually the right way to do things in Julia. Using abstract types and method dispatch would be the recommended solution that leads to clearer / faster / more “Julian” code.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 27, 2020, 3:56am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/9 "2020-09-27T03:56:10Z")

</div>

> [@marius311](#):
>
> but I would keep in mind that this is not usually the right way to do things in Julia.

Exactly.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 27, 2020, 3:58am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/10 "2020-09-27T03:58:50Z")

</div>

@marius311 Thanks for your advice.

Many people using Julia give advices like you.  
Unfortunately, I cannot find a both easy (to me) and Julian way to write codes although I’ve spent long time here.

I’ll read the post you suggested again and think about it deeply.  
Thank you all!

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 27, 2020, 4:01am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/11 "2020-09-27T04:01:34Z")

</div>

If u r able, get the book and read thru it? [https://subscription.packtpub.com/book/programming/9781838648817/8/ch08lvl1sec38/the-delegation-pattern](https://subscription.packtpub.com/book/programming/9781838648817/8/ch08lvl1sec38/the-delegation-pattern)

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [September 27, 2020, 5:23am UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/12 "2020-09-27T05:23:27Z")

</div>

@xiaodai  
I’ve just read the book.  
It’s helpful, and (probably) summarises the above stuffs.  
+) I rethink that the feature I needed is REALLY necessary for me.  
Great! 🙂

---

<div class="post-metadata">

**Author:** ![Marius\_Kruger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius_kruger/32/904_2.png) [@Marius\_Kruger](https://discourse.julialang.org/u/Marius_Kruger)\
**Post date:** [April 3, 2023, 10:48pm UTC](https://discourse.julialang.org/t/generate-a-mutable-struct-which-inherits-others/47347/13 "2023-04-03T22:48:50Z")

</div>

Similar to what @marius311 mentioned. I use [GitHub - ipod825/OOPMacro.jl: Object Oriented Programming in Julia](https://github.com/ipod825/OOPMacro.jl) to get around this.
