# Macro to inherit fields from one type to another

**URL:** <https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346>\
**Category:** New to Julia\
**Created:** [February 9, 2023, 12:59pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346 "2023-02-09T12:59:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamath\_Laxminarayana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamath_laxminarayana/32/46735_2.png) [@Kamath\_Laxminarayana](https://discourse.julialang.org/u/Kamath_Laxminarayana)\
**Post date:** [February 9, 2023, 12:59pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/1 "2023-02-09T12:59:35Z")

</div>

Hi, I am very new to Julia . Unsurprisingly, I was irritated by the fact that fields are not inherited from a base struct to the other. I clearly know such inheritance is not always desired,. But in some cases, it becomes quite useful, like when writing widgeting system (you dont want to declare posx, posy, width, height, children, onclick, ondragstart, and so on and so forth in every other widget you want)

Hence, I tried writing a macro to do the same. Please let me know if there are any issues. The usage is given later. The usage style could be a bit counterintuitive to some.

Please comment if the following looks good, or if there are any big gotchas. I also think this could be useful to others

```julia
#extendit.jl
macro inherit_fields(thetype)
    theRealType = eval(thetype)
    eArray = []
    for (thefieldname, thefieldtype) in zip(fieldnames(theRealType), fieldtypes(theRealType))
        push!(eArray, Expr(:(::), thefieldname, thefieldtype))
    end
    return esc(Expr(:block, eArray...))
end

```

How to use this :

```julia
#tryextending.jl
include("./extendit.jl")

struct A1
a1::Int
end

struct A2
@inherit_fields(A1)
a2::Int
end

struct A3
@inherit_fields(A2)
a3::Int
end

dump(A3)

```

Output

```bash
geek@geekbox:/home/geek$ julia tryextendingit.jl 
A3 <: Any
  a1::Int64
  a2::Int64
  a3::Int64

```

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [February 9, 2023, 1:09pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/2 "2023-02-09T13:09:42Z")

</div>

Check out [ObjectOriented.jl](https://github.com/Suzhou-Tongyuan/ObjectOriented.jl)!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 9, 2023, 1:10pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/3 "2023-02-09T13:10:03Z")

</div>

> [@Kamath\_Laxminarayana](#):
>
> (you dont want to declare posx, posy, width, height, children, onclick, ondragstart, and so on and so forth in every other widget you want)

If you have a lot of common parameters, can’t they be collected in their own type, `WidgetParams`, which you wrap inside your individual widget types? Seems like repeating fields everywhere may violate DRY.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [February 9, 2023, 1:17pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/4 "2023-02-09T13:17:04Z")

</div>

I’m not at my computer right now, but I think you’ll get an error if two property names are the same. I think if you really, really want inheritance, then the package I linked has already solved this problem. No need to reinvent the wheel.

But in general, @DNF is correct that if you have a common set of parameters, then just make that a struct that can be a field of all widgets. The. You can access fields with `mywidget.params.xpos`.

---

<div class="post-metadata">

**Author:** ![Kamath\_Laxminarayana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamath_laxminarayana/32/46735_2.png) [@Kamath\_Laxminarayana](https://discourse.julialang.org/u/Kamath_Laxminarayana)\
**Post date:** [February 9, 2023, 1:48pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/5 "2023-02-09T13:48:35Z")

</div>

🤯 🤯 🤯 Thank you so much !!!

Why is this not in [juliapackages.org](http://juliapackages.org) ?! Any collection of awesome Julia libraries or projects like this?

---

<div class="post-metadata">

**Author:** ![Kamath\_Laxminarayana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamath_laxminarayana/32/46735_2.png) [@Kamath\_Laxminarayana](https://discourse.julialang.org/u/Kamath_Laxminarayana)\
**Post date:** [February 9, 2023, 1:51pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/6 "2023-02-09T13:51:45Z")

</div>

That is another way to look at it. I did think about it actually, as a fallback to this macro. But inheritance generally makes the variable names consistent, and it is easy to write multiple dispatch functions this way. Lesser the code, lesser the bugs

---

<div class="post-metadata">

**Author:** ![Kamath\_Laxminarayana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamath_laxminarayana/32/46735_2.png) [@Kamath\_Laxminarayana](https://discourse.julialang.org/u/Kamath_Laxminarayana)\
**Post date:** [February 9, 2023, 1:52pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/7 "2023-02-09T13:52:21Z")

</div>

Thanks again, will check.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 9, 2023, 2:14pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/8 "2023-02-09T14:14:18Z")

</div>

A common pattern is to use auxiliary functions like:

```julia
xpos(w::AbstractWidget) = w.params.xpos

```

to access the fields of an enclosed structure, and sometimes corresponding specific methods for special widgets that may or may not have one property.

Nothing against trying a more object-oriented structure, but sometimes it is painful to fight against the most natural language patterns.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 9, 2023, 2:16pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/9 "2023-02-09T14:16:30Z")

</div>

> [@Kamath\_Laxminarayana](#):
>
> But inheritance generally makes the variable names consistent, and it is easy to write multiple dispatch functions this way. Lesser the code, lesser the bugs

I’m not seeing the disadvantage of an aggregated parameter field. It should be equally consistent, and no more problematic for multiple dispatch.

---

<div class="post-metadata">

**Author:** ![Kamath\_Laxminarayana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kamath_laxminarayana/32/46735_2.png) [@Kamath\_Laxminarayana](https://discourse.julialang.org/u/Kamath_Laxminarayana)\
**Post date:** [February 9, 2023, 2:59pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/10 "2023-02-09T14:59:33Z")

</div>

Yeah, now that I think of it, Maybe you are right. Will give all different paradigms a try. Only a week since I started learning Julia.

Either way, Just bowled over by how nice, easy and powerful its Meta-programming is. I actually also loved the Julian way of multiple dispatch over regular OOPs . Only thing I hadn’t liked is not being able to share constructors and needing to re enter fields over and over.

But now those seems to be solved … thanks for all the help.  
(Need to tell my wife what Julia is. Else will be in trouble if I suddenly scream I ❤ Julia)

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [February 9, 2023, 3:02pm UTC](https://discourse.julialang.org/t/macro-to-inherit-fields-from-one-type-to-another/94346/11 "2023-02-09T15:02:32Z")

</div>

Glad you got it working and welcome! Yes, my partner has been confused more than once when I’m talking about Julia 😂
