# Aliasing two structs and having them be subtypes of an abstract type?

**URL:** <https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410>\
**Category:** General Usage\
**Tags:** type, data\_structures, struct\
**Created:** [September 29, 2023, 7:11pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410 "2023-09-29T19:11:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![linkz](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@linkz](https://discourse.julialang.org/u/linkz)\
**Post date:** [September 29, 2023, 7:11pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/1 "2023-09-29T19:11:31Z")

</div>

I am wondering if when I have two structs, say

```julia
mutable struct A{a, b}
   str_a::a
   str_b::b
end

mutable struct B{a, b, c}
   str_a::a
   str_b::b
   str_c::c
end

```

and I alias

```julia
const A_num = A{<:Number}
const B_num = B{<:Number}

```

is it possible to create an abstract type, such that `A_num` and `B_num` would be its subtypes without `A` and `B` being subtypes of some abstract type?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 29, 2023, 7:19pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/2 "2023-09-29T19:19:12Z")

</div>

This is not possible.

---

<div class="post-metadata">

**Author:** ![linkz](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@linkz](https://discourse.julialang.org/u/linkz)\
**Post date:** [September 29, 2023, 7:29pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/3 "2023-09-29T19:29:46Z")

</div>

Well that’s a shame. Would `A` and `B` have to be subtypes of the same abstract type for it to be possible?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 29, 2023, 7:30pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/4 "2023-09-29T19:30:42Z")

</div>

yeah.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 29, 2023, 7:33pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/5 "2023-09-29T19:33:08Z")

</div>

Could you explain the bigger picture? What are you trying to acccomplish?

---

<div class="post-metadata">

**Author:** ![linkz](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@linkz](https://discourse.julialang.org/u/linkz)\
**Post date:** [September 29, 2023, 7:36pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/6 "2023-09-29T19:36:32Z")

</div>

Basically trying to avoid code duplication due to `A` and `B` being different types. I was hoping I could somehow hack an abstract type, say `C`, of which `A` and `B` would be a subtype and have `f(x::C)`, rather than `f(x::A)` and `f(x::B)` (but for a lot of functions).

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 29, 2023, 7:37pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/7 "2023-09-29T19:37:25Z")

</div>

why do you need to add a type parameter at all?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 29, 2023, 7:38pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/8 "2023-09-29T19:38:39Z")

</div>

Create a box type as follows. Does that help?

```julia
julia> abstract type AbstractBox end

julia> struct Box{T} <: AbstractBox
           x::T
           Box{T}(args...) where T= new{T}(T(args...))
       end

julia> Base.getproperty(b::Box, s::Symbol) = getproperty(getfield(b,:x), s)

julia> mutable struct A{a, b}
          str_a::a
          str_b::b
       end

julia> mutable struct B{a, b, c}
          str_a::a
          str_b::b
          str_c::c
       end

julia> const A_boxed = Box{A}
A_boxed (alias for Box{A})

julia> const B_boxed = Box{B}
B_boxed (alias for Box{B})

julia> x = A_boxed(1,2)
A_boxed(A{Int64, Int64}(1, 2))

julia> y = B_boxed(3,4,5)
B_boxed(B{Int64, Int64, Int64}(3, 4, 5))

julia> x isa AbstractBox
true

julia> y isa AbstractBox
true

julia> x.str_a
1

julia> x.str_b
2

julia> y.str_a
3

julia> y.str_b
4

julia> y.str_c
5

```

**Edit** : For completeness and tab completion:

```julia
Base.propertynames(x::Box) = Base.propertynames(getfield(x, :x))

```

---

<div class="post-metadata">

**Author:** ![linkz](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@linkz](https://discourse.julialang.org/u/linkz)\
**Post date:** [September 29, 2023, 7:40pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/9 "2023-09-29T19:40:03Z")

</div>

It’s a complex codebase with lots of functions that depending on the input type would have to do different things in certain parts.

---

<div class="post-metadata">

**Author:** ![linkz](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@linkz](https://discourse.julialang.org/u/linkz)\
**Post date:** [October 2, 2023, 9:30am UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/10 "2023-10-02T09:30:45Z")

</div>

This is _very close_ to what I need, except that I can’t really do (otherwise it breaks with the rest of the code that is)

```julia
x = A_boxed(1, 2)
y = B_boxed(3, 4, 5)

```

but I have to keep

```julia
x = A_boxed(1, 2)
y = B_boxed(3, 4, 5)

```

Thank you for your attempts, but I think I’ll pursue a different approach.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 2, 2023, 1:46pm UTC](https://discourse.julialang.org/t/aliasing-two-structs-and-having-them-be-subtypes-of-an-abstract-type/104410/11 "2023-10-02T13:46:10Z")

</div>

You can always do

```julia
julia> mutable struct A{a, b}
          str_a::a
          str_b::b
       end

julia> mutable struct B{a, b, c}
          str_a::a
          str_b::b
          str_c::c
       end
julia> const A_num = A{<:Number, <:Number}
A_num (alias for A{<:Number, <:Number})

julia> const B_num = B{<:Number, <:Number, <:Number}
B_num (alias for B{<:Number, <:Number, <:Number})

julia> const AB_num = Union{A_num, B_num}
AB_num (alias for Union{A{<:Number, <:Number}, B{<:Number, <:Number, <:Number}})

julia> A("", 2) isa AB_num
false

julia> B(1//2, 2, 3/0) isa AB_num
true

julia> B(1//2, 2, 3/0)
B_num{Rational{Int64}, Int64, Float64}(1//2, 2, Inf)

```

What you cannot do is have one package create an abstract type encompassing both `A_num` and `B_num` (`AB_num` in the example) and then have another package define a new subtype `struct C_num <: AB_num`. For that kind of stuff, you should look at Holy traits.
