# Types with multiple supertypes

**URL:** <https://discourse.julialang.org/t/types-with-multiple-supertypes/58787>\
**Category:** General Usage\
**Created:** [April 7, 2021, 7:01pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787 "2021-04-07T19:01:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [April 7, 2021, 7:01pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787/1 "2021-04-07T19:01:48Z")

</div>

Hey everybody,

I have a model in which I can have different setups. There are in principle 2x2 combinations. Let’s say models can be A1, A2, B1, B2.

I now have types that are specialized for each type. Some function behaviour, however, is common to A-models, whereas other function behave the same for 1-models. For some a specialized method for A1, A2, etc. is necessary.

Is it possible to define types with multiple supertypes? For example, A1 \<: A, but also A1 \<: 1. If that is not possible, what kind of design pattern would you suggest?

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [April 7, 2021, 7:12pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787/2 "2021-04-07T19:12:01Z")

</div>

_Edit — to answer the original question: no, that kind of multiple “inheritance” is not possible._

You could use type parameters:

```julia
struct Model{S, T}
    ...
end

A1object = Model{:A, :1}()

B1object = Model{:B, :1}()

A2object = Model{:A, :2}()

B2object = Model{:B, :2}()

```

and then dispatch the desired behavior based on the two parameters:

```julia
f(obj::Model{S, :1}) where S = ... # only applies to the 1s
f(obj::Model{:A, T}) where T = ... # only applies to the As

```

and so on.

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [April 7, 2021, 7:23pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787/3 "2021-04-07T19:23:28Z")

</div>

Thank you, I’ll keep that in mind when I refactor my code the next time 🙂

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [April 7, 2021, 7:27pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787/4 "2021-04-07T19:27:03Z")

</div>

I would use multiple dispatch passing both model types as variables. Example:

```julia
abstract type LetterModel end
abstract type NumberModel end

struct A <: LetterModel
struct Num1 <: NumberModel

f(letter::LetterModel, number::NumberModel) # generic implementation
f(letter::A, number::NumberModel) # uses A, but ignores Number
f(letter::B, number::Num2) # Specialized method on both B and 2

```

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [April 7, 2021, 7:33pm UTC](https://discourse.julialang.org/t/types-with-multiple-supertypes/58787/5 "2021-04-07T19:33:44Z")

</div>

Thank you, after some thinking, this is what I will do. In my case, my model is indeed defined by two sets of parameters. One set is related to the letter component, whereas the other one is related to the number component. Then I can define functions just the way you did 🙂
