# Declare a structure as a subtype of another structure

**URL:** <https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611>\
**Category:** General Usage\
**Created:** [May 10, 2023, 1:20pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611 "2023-05-10T13:20:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 10, 2023, 1:20pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611/1 "2023-05-10T13:20:41Z")

</div>

Curiously didn’t find anything on this, even after some decent amount of googling…

I have a structure, and then I have a version of it with some additional info. Can I make the latter inherit from the former? Naively, I thought this would work:

```julia
struct MyStruct
    a::Int64
end

struct ExpandedMyStruct <: MyStruct
    a::Int64 # Tried both with and without this line
    b::Int64
end

```

but this yields a `invalid subtyping in definition of ExpandedMyStruct` error.

The reason I want to do this is that I have a function (that works on `MyStruct`), e.g.

```julia
function f(ms::MyStruct)
    println(ms.a)
end

f(MyStruct(2))

```

and I want to make this work on `ExpandedMyStruct` by default (without having to declare it as `function f(ms::Union{MyStruct,ExpandedMyStruct)`. Does what I am trying to do make sense?

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [May 10, 2023, 1:28pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611/2 "2023-05-10T13:28:54Z")

</div>

How about this?

```julia
julia> abstract type MyAbstract end

julia> struct MyStruct <: MyAbstract
           a::Int
       end

julia> struct AlsoMyStruct <: MyAbstract
           a::Int
       end

julia> f(x::MyAbstract) = x.a
f (generic function with 1 method)

julia> f(MyStruct(3))
3

julia> f(AlsoMyStruct(3))
3

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 10, 2023, 1:35pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611/3 "2023-05-10T13:35:11Z")

</div>

Looks good. Is there a reason for this, seemingly a bit convoluted, way of doing it? Tried to find stuff about this in the Julia doc, but did;t find anything.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [May 10, 2023, 1:44pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611/4 "2023-05-10T13:44:17Z")

</div>

Yes, it is discussed in the manual [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/)

> One particularly distinctive feature of Julia’s type system is that concrete types may not subtype each other: all concrete types are final and may only have abstract types as their supertypes. While this might at first seem unduly restrictive, it has many beneficial consequences with surprisingly few drawbacks. It turns out that being able to inherit behavior is much more important than being able to inherit structure, and inheriting both causes significant difficulties in traditional object-oriented languages.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 10, 2023, 1:49pm UTC](https://discourse.julialang.org/t/declare-a-structure-as-a-subtype-of-another-structure/98611/5 "2023-05-10T13:49:52Z")

</div>

Thanks!
