# How to make an abstract type as a super type of composite types

**URL:** https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174
**Category:** New to Julia
**Created:** [August 5, 2019, 11:17am UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174 "2019-08-05T11:17:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)
#### Post date: [August 5, 2019, 11:17am UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/1 "2019-08-05T11:17:47Z")

</div>

Hi,

I would like to create an abstract type, which is a supertype of several composite types, which I coded in modules, so that a function just works for the composite types, which have this specific abstract type as supertype. Furthermore I would like to use the package Reexport for loading all modules in just one module.

```julia
module MyModuleA

mutable struct MyTypeA
  var1
  var2
  ....
end

end

module MyModuleB

mutable struct MyTypeB
  var1
  var2
  ....
end

end

module MyModuleC

mutable struct MyTypeC
  var1
  var2
  ....
end

end

```

And MyModuleD should reexport the module MyModuleA, MyModuleB and MyModuleC and the abstract type MyType D should be the super type of MyTypeA, MyTypeB and MyTypeC

```julia
module MyModuleD

using Reexport

abstract type MyType D end

@reexport using MyModuleA
@reexport using MyModuleB
@reexport using MyModuleC

function my_function(var::MyTypeD)
   ....
end

end

```

My Problem is now:  
When I use MyModuleD in MyModuleA, MyModuleB or MyModuleC to get the definition of the abstract type MyTypeD, that I get a warning "Replacing module `MyModuleA` because of the reexport in MyModule D.

Does somebody know how I have to code this?

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 5, 2019, 11:33am UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/2 "2019-08-05T11:33:47Z")

</div>

To make D the supertype, you need to do something like

```julia
mutable struct A <: D
# ...
end

```

But this means you need to have D defined prior to the definition of A. As far as I know, there’s now way to make something the subtype of something else after the fact.

In your case, this may mean importing D in modules A-C, so I’m not sure the module structure you want is possible. Then again, I never use nested modules like that, so take that with a hefty grain of salt.

Edit: given your edit, seems like you already knew this, ignore me 🤨

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)
#### Post date: [August 5, 2019, 11:36am UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/3 "2019-08-05T11:36:38Z")

</div>

Ok, thanks. So it would be better to just include the code in one big module? My construction works, but it leads to annoying warnings. And I think this isn´t the Julian way to do it 😉

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [August 5, 2019, 1:37pm UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/4 "2019-08-05T13:37:27Z")

</div>

Reexporting stuff does not seem to be needed here; moreover if you really need to load all modules at once all the time, maybe they should not be modules. Anyway, an easy way to handle this is to define the abstract type from module D first, then use it in the rest of the modules (A,B,C as @kevbonham mentioned ) and not re-export anything; Your actual code will be contain

```julia
using MyModuleA, MyModuleB, MyModuleC
using MyModuleD # if the abstract type is needed
...

```

PS If you really need the namespace or a hierarchical structure of sorts, A,B,C could be submodules of D.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [August 5, 2019, 4:03pm UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/5 "2019-08-05T16:03:02Z")

</div>

I would probably define all types in `MyModule D`:

```julia
abstract type MyTypeD end

mutable struct MyTypeA <: MyTypeD
<more>
end

```

Then put the code that does the work on each type in separate modules that use `MyModuleD`.

---

<div class="post-metadata">

### Author: ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)
#### Post date: [August 6, 2019, 11:04am UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/6 "2019-08-06T11:04:35Z")

</div>

Thanks for your replies. Most of the time I would like to use the modules separately, but there are some functions, which should work on each module. Furthermore it would be useful, but not necessary, when I use MyModuleD that I can also use the structs and methods of the modules A, B and C within the same hierarchical structure. So that I can use

```julia
my_struct_a = MyModuleA.MyStructA(var1,....)

```

with using MyModuleD or MyModuleA.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [August 8, 2019, 12:54pm UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/7 "2019-08-08T12:54:31Z")

</div>

Could you define your `abstract type` and the sub-types in `modules MyTypes`.  
Then modules A, B, C use `MyTypes` and `D` uses A, B, C and MyTypes?  
I.e., separate out type definitions into their own module?

---

<div class="post-metadata">

### Author: ![janrpeters](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@janrpeters](https://discourse.julialang.org/u/janrpeters)
#### Post date: [June 11, 2020, 7:07pm UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/8 "2020-06-11T19:07:17Z")

</div>

I would love to do this

> [@hendri54](#):
>
> ```julia
> abstract type MyTypeD end
> 
> mutable struct MyTypeA <: MyTypeD
> <more>
> end
> 
> ```

the other way around

```julia
mutable struct MyTypeA 
<more>
end

abstract type MyTypeB <: MyTypeA end
abstract type MyTypeC <: MyTypeA end

```

but I cannot figure out how.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [June 11, 2020, 11:49pm UTC](https://discourse.julialang.org/t/how-to-make-an-abstract-type-as-a-super-type-of-composite-types/27174/9 "2020-06-11T23:49:58Z")

</div>

> [@janrpeters](#):
>
> ```julia
> abstract type MyTypeB <: MyTypeA end
> abstract type MyTypeC <: MyTypeA end
> 
> ```

Did you mean to reverse the “inequalities” here:

```julia
abstract type MyTypeB >: MyTypeA end
abstract type MyTypeC >: MyTypeA end

```

In other words, are you trying to define a `struct` and “later” (in another module that may not be used in all calls to the `struct`) make it a subtype of something else? I don’t think this is possible.

My conjecture is that you are doing this so that dispatch can be written based on the abstract type? Then you should not use a type hierarchy but traits. [BinaryTraits.jl](https://discourse.julialang.org/t/ann-binarytraits-jl-a-new-traits-package/37475) is a nice implementation, but there are others.
