# Multiple Parent Types: How to go about it?

**URL:** <https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493>\
**Category:** General Usage\
**Tags:** question, inheritance\
**Created:** [August 16, 2021, 3:15pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493 "2021-08-16T15:15:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 16, 2021, 3:15pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/1 "2021-08-16T15:15:45Z")

</div>

There are many cases where we would like to assign multiple parents to a given type, e.g:

```julia
abstract type Person end

abstract type Soldier <: Person end
abstract type Civilian <: Person end

abstract type Man <: Person end
abstract type Woman <: Person end

struct PrivateClubMember <: Woman, Soldier end

```

The idea is to be able to write functions for `Woman` and `Soldier` that any `PrivateClubMember` could default to, e.g.

```julia
info(pcm::Woman) = ...
rank(pcm::Soldier) = ...

```

and have an error if we call a function shared by Parent types, e.g:

```julia
info(pcm::Soldier) = ...
pcm = PrivateClubMember()

info(pcm)
ERROR: functions shared by parent types must be explicitly defined 
       for sub-types. 

```

and fix this situation with

```julia
info(pcm::PrivateClubMember) = ...

```

My understanding is that as of today Julia does not support multiple parent types, I wonder:

- **Is this something Julia will support for Julia 2.0, 3.0… ever?**
- **Is there currently a standard Julia way to handle these scenarios?**

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [August 16, 2021, 3:41pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/2 "2021-08-16T15:41:01Z")

</div>

In a lot of cases where you need inheritance but there’s not a neat type hierarchy, traits can be a good solution. There’s a nice intro by @oxinabox [here](https://invenia.github.io/blog/2019/11/06/julialang-features-part-2/).

There are also a few packages oriented toward helping with this approach:

- [SimpleTraits](https://github.com/mauro3/SimpleTraits.jl) by @mauro3
- [BinaryTraits](https://github.com/tk3369/BinaryTraits.jl) by @tk3369
- [TraitWrappers](https://github.com/xiaodaigh/TraitWrappers.jl) by @xiaodai
- [WhereTraits](https://github.com/schlichtanders/WhereTraits.jl) by @schlichtanders
- [CanonicalTraits](https://github.com/thautwarm/CanonicalTraits.jl) by @thautwarm

These are at varying degrees of stability, so I’d ask around before committing to any one package.

EDIT: This was the first thing that came to mind, but @tbeason 's suggestion of reorganization is probably better if you can make it work with your situation.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 16, 2021, 3:45pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/3 "2021-08-16T15:45:59Z")

</div>

In a lot of cases I’d guess you can just reorg what you’ve written to make it work.

```julia
abstract struct AbstractJobType end
struct Soldier <: AbstractJobType end
struct Civilian <: AbstractJobType end

abstract struct AbstractSex end
struct Male <: AbstractSex end
struct Female <: AbstractSex end

struct Person{J,S} where {J<:AbstractJobType, S<:AbstractSex}
job::J
sex::S
end

```

and then when defining methods you define them somewhat explicitly

```julia
info(p::Person{Soldier,Male}) = ...

```

or something to this effect.

Hopefully someone with more knowledge on this style can clarify or correct this answer.

---

<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:** [August 16, 2021, 3:46pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/4 "2021-08-16T15:46:34Z")

</div>

> [@viraltux](#):
>
> Is this something Julia will support for Julia 2.0, 3.0… ever?

Multiple inheritance, probably never. I cannot dig up a cite now, but I think it is widely considered to be undesirable.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 16, 2021, 3:47pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/5 "2021-08-16T15:47:35Z")

</div>

> [@viraltux](#):
>
> does not support multiple parent types

multiple inheritance is unlikely to be ever supported, probably to the similar degree of support extending concrete types…

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 16, 2021, 4:08pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/6 "2021-08-16T16:08:42Z")

</div>

> [@DNF](#):
>
> Multiple inheritance, probably never. I cannot dig up a cite now, but I think it is widely considered to be undesirable.

> [@jling](#):
>
> multiple inheritance is unlikely to be ever supported, probably to the similar degree of support extending concrete types…

I don’t know about widely “undesirable” but at least not by some of the Julia founders: [abstract multiple inheritance · Issue #5 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5)

Never is a long time… but you guys might be right.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 16, 2021, 4:11pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/7 "2021-08-16T16:11:09Z")

</div>

> [@tbeason](#):
>
> In a lot of cases I’d guess you can just reorg what you’ve written to make it work.

Thank you! I’ll have a go to this approach and see how it goes!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 16, 2021, 4:41pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/8 "2021-08-16T16:41:54Z")

</div>

if you read that you will see a trait system is more likely. Which is understandable as Rust picked up nicely and people already implemented toy Trait system using current type system.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 16, 2021, 6:10pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/9 "2021-08-16T18:10:06Z")

</div>

> [@jling](#):
>
> if you read that you will see a trait system is more likely. Which is understandable as Rust picked up nicely and people already implemented toy Trait system using current type system.

I’m not sure what part of that discussion makes you think that traits are more likely. In my opinion most comments go in the direction that multiple parent types are great but hard to implement and that traits are a limited solution.

Also, why we need to care about Rust decisions in Julia? Are there any interdependencies I’m not aware of between the two projects?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [August 16, 2021, 7:55pm UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/10 "2021-08-16T19:55:39Z")

</div>

There are no interdependencies or anything of the sort (well both use LLVM but that goes for many others). It’s just that looking at the implementations of others is a nice way to evaluate how they work and what implementations are out there, if there is something very cool out there, it’s always worth it to copy it.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 17, 2021, 6:22am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/11 "2021-08-17T06:22:31Z")

</div>

> [@gbaraldi](#):
>
> There are no interdependencies or anything of the sort (well both use LLVM but that goes for many others). It’s just that looking at the implementations of others is a nice way to evaluate how they work and what implementations are out there, if there is something very cool out there, it’s always worth it to copy it.

My understanding, roughly speaking, is that Rust is a language created by software engineers for software engineers and Julia is a language created by software engineers for all sort of engineers and scientists. When it comes to languages to draw inspiration from for Julia, in this context, maybe Matlab is more appropriate [Subclassing Multiple Classes - MATLAB & Simulink](https://www.mathworks.com/help/matlab/matlab_oop/subclassing-multiple-classes.html)

In this line of scientific goals for Julia, among the comments supporting multiple parents I found particularly interesting this one explaining the benefits for scientific projects [abstract multiple inheritance · Issue #5 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5#issuecomment-271773458).

Anyway, the link I shared was a very old discussion and I was wondering if any decisions have been taken by the Julia team.

---

<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:** [August 17, 2021, 7:36am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/12 "2021-08-17T07:36:45Z")

</div>

> [@viraltux](#):
>
> When it comes to languages to draw inspiration from for Julia, in this context, maybe Matlab is more appropriate

😬

I think the reason for taking inspiration from Rust is that it is a new language that is considered to be modern and well-designed and using best practices for many things (I personally don’t know much about Rust.)

Matlab, on the other hand, is the language that many Julia users are _running away from_ because it is considered _badly designed_ and outdated, and really horrible to work with, beyond small throw-away scripts (and I _do_ know a bit about Matlab, as I have used it every day for \>20 years.)

Taking inspiration from Matlab beyond surface syntax (which is ok) is _definitely not_ something I would like to see.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 17, 2021, 7:47am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/13 "2021-08-17T07:47:52Z")

</div>

> [@DNF](#):
>
> Taking inspiration from Matlab beyond surface syntax (which is ok) is _definitely not_ something I would like to see.

That’s what I meant (syntax), and **in that context** I would also advice R. User experience is very important besides well designed internals. Types with multiple parents affect internals but it definitely affects syntax as well.

---

<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:** [August 17, 2021, 7:51am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/14 "2021-08-17T07:51:46Z")

</div>

> [@viraltux](#):
>
> That’s what I meant

I though you were advocating taking inspiration from Matlab language design, because it has multiple inheritance. (Multiple inheritance in general is hugely controversial, afaik.)

> [@viraltux](#):
>
> and **in that context** I would also advice R

R? Well, this language I don’t know so well, but everything I’ve heard points to it being pretty far away from Julia design goals.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 17, 2021, 7:57am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/15 "2021-08-17T07:57:17Z")

</div>

> [@DNF](#):
>
> I though you were advocating taking inspiration from Matlab language design, because it has multiple inheritance. (Multiple inheritance in general is hugely controversial, afaik)

Multiple Inheritance is also a **syntax** design, and controversial because it is hard to implement maybe, but if you check the link with the discussion many people like it, including key people in the Julia project.

> [@DNF](#):
>
> R? Well, this language I don’t know so well, but everything I’ve heard points to it being pretty far away from Julia design goals.

Internal design absolutely, user experience quite the opposite.

---

<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:** [August 17, 2021, 8:13am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/16 "2021-08-17T08:13:55Z")

</div>

> [@viraltux](#):
>
> **syntax** design, and controversial because it is hard to implement maybe

I won’t be to confident here, but my understanding is that it leads to badly designed programs. I’ve _never_ heard it has anything to do with difficulty of implementation, only that it is a bad idea.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 17, 2021, 8:17am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/17 "2021-08-17T08:17:52Z")

</div>

> [@DNF](#):
>
> I won’t be to confident here, but my understanding is that it leads to badly designed programs. I’ve _never_ heard it has anything to do with difficulty of implementation, only that it is a bad idea.

From @jeff.bezanson

> I agree we want it [Multiple Inheritance], but realistically it will take a bunch of time to settle and we know julia is perfectly usable without it.

> <https://github.com/JuliaLang/julia/issues/5#issuecomment-1285388>
>
> In an email discussion we came to the conclusion that it made sense to have mult…iple inheritance in Julia with one fairly simple restriction:
> 
> \> If two abstract types are are used for dispatch in the same "slot" of the same generic function object then they cannot share a common, concrete descendant (all types share \`None\` as a common abstract descendant).
> 
> This restriction, together with Julia not allowing inheritance from non-abstract types, seems to address all the practical issues one typically encounters with multiple inheritance. The following, for example, would be disallowed:
> 
> \`\`\`
> abstract A
> abstract B
> 
> type C \<: A, B
> end
> 
> f(A) = 1
> f(B) = 2 # ERROR: A and B share a common descendant
> \`\`\`
> 
> Note that a generic function is an object external to all types, not a name inside of a type as it would be in a traditional object-orientation language. Thus, one can have \`f(a::A)\` in one namespace and \`f(b::B)\` in another namespace without problems, so long as the \`f\`s in these two namespaces are distinct generic function objects.

---

<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:** [August 17, 2021, 8:21am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/18 "2021-08-17T08:21:53Z")

</div>

Well, this is over a decade ago, and before the language was pre-released. It’s issue number 5 (out of 41700), was it even before the introduction of multiple dispatch?

I think the general opinion long ago moved to preferring traits and composition.

OK, scrolling down on that issue, I see there’s still some discussion, up to a couple of years ago. Anyway, I’ll try not to be too confident, I’ve just always read that “multiple inheritance is _bad_” as basically a consensus opinion, so I’m a bit surprised that it is seriously considered.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 17, 2021, 8:36am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/19 "2021-08-17T08:36:37Z")

</div>

> [@viraltux](#):
>
> My understanding, roughly speaking, is that Rust is a language created by software engineers for software engineers and Julia is a language created by software engineers for all sort of engineers and scientists. When it comes to languages to draw inspiration from for Julia, in this context, maybe Matlab is more appropriate [Subclassing Multiple Classes - MATLAB & Simulink](https://www.mathworks.com/help/matlab/matlab_oop/subclassing-multiple-classes.html)

I’m afraid I don’t follow, why should “who the language is designed for” have any bearing on where to draw inspiration/good features from?

Rust is not specifically written for software engineers, it was built to eliminate security related bugs in the (then used) Servo layouting engine (the thing that draws a website in your browser) for Firefox, which originally was C++. Since then it has grown to all kinds of disciplines, mostly systems related, because _you literally can’t compile programs with out of bounds memory access_ unless you use `unsafe`. It’s grown in features and scope because once C/C++/systems programmers got a taste of a language with a few nice features making their lives easier, we suddenly decided to have our cake and eat it too. In this regard, Rust is not much different from Julia philosophically, we’re just coming at it from a different angle.

> [@viraltux](#):
>
> Multiple Inheritance is also a **syntax** design, and controversial because it is hard to implement maybe, but if you check the link with the discussion many people like it, including key people in the Julia project.

Syntax is superficial, it’s semantics that matter. If anything, I doubt the surface syntax of julia would change a whole lot due to the addition of “multiple inheritance”. The issue #5 you linked is by now very old, back then julia looked very different in its 0.2(?) days.

* * *

When talking about “inheritance”, there’s (from what I can tell) two main aspects relevant here:

- Inheritance of data/layout, which means inheriting fields from supertypes (i.e., allowing concrete type to be subtyped).
- Inheritance of behavior, which means allowing a type to match method signatures where any supertype of that type is specified.

The former will, which I’m very confident about, not change. Having only abstract types be inheritable and thus have only concrete types with a data layout makes optimizations regarding code/stack layout easy, makes a lot of data be inlineable etc. This is where “R style” inheritance breaks down, because in that model subtypes _do_ inherit parent properties (which can lead to conflicts and all other sorts of headaches, which should be familiar to people used to that style of inheritance. It’s similar to how inheritance in Java works).

The latter is (to a certain extent) already a thing in julia. When you have a type tree like `Any >: AbstractA >: SubAbstractB >: ConcreteC`, `ConcreteC` can already be used in any place where either `AbstractA` or `SubAbstractB` is specified. It thus “inherits” the behavior of both `AbstractA` as well as `SubAbstractB`. The addition of more multiple behavioral inheritance and the semantics of that is what issue #5 is about, not necessarily how this should be written.

As for traits vs. multiple inheritance - Github has the annoying “feature” of hiding “irrelevant” discussion in issues. I hope you’ve read all of those hidden comments, and not just the part Github shows by default (which misleadingly would give the impression that multiple inheritance in the classic sense is the way to go). For example, in [abstract multiple inheritance · Issue #5 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5#issuecomment-123338906) there are a few links about more discussion/information/problems that crop up when thinking about this (and why traits will most likely be the way to go), including [jeff’s Phd thesis](https://github.com/JeffBezanson/phdthesis/blob/master/main.pdf). The discussion in #5 turns to traits as the desirable thing some time after 2014 - looking at how Rust is doing it only follows naturally.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 24, 2021, 8:20am UTC](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493/20 "2021-08-24T08:20:48Z")

</div>

> [@viraltux](#):
>
> > I agree we want it [Multiple Inheritance], but realistically it will take a bunch of time to settle and we know julia is perfectly usable without it.

As others have said, the discussion has moved on since, and traits seem like a much better solution.

I think that it would make sense to revisit these old issues periodically, and close them; or at least update them with a summary. Issues older than 5 years usually have a very low probability of being relevant for current priorities.

[Next page](https://discourse.julialang.org/t/multiple-parent-types-how-to-go-about-it/66493.md?page=2)
