# A question on the type system

**URL:** <https://discourse.julialang.org/t/a-question-on-the-type-system/109624>\
**Category:** General Usage\
**Tags:** type\
**Created:** [February 2, 2024, 3:00pm UTC](https://discourse.julialang.org/t/a-question-on-the-type-system/109624 "2024-02-02T15:00:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 2, 2024, 3:00pm UTC](https://discourse.julialang.org/t/a-question-on-the-type-system/109624/1 "2024-02-02T15:00:55Z")

</div>

Hello!

Say I have two nested(?) structures as

```Julia
mutable struct MyInnerStruct
    index :: Int64
    vector :: Vector{Float64}
    boolean_flag :: Bool
    a :: Float64
    b :: Float64
    c :: Float64
end

struct MyOuterStruct
    name :: String
    vector :: Vector{MyInnerStruct}
end

```

and at run time I have

```Julia
mystruct = MyOuterStruct("my_out_struct", [MyInnerStruct(i, rand(100), rand(Bool), rand(1), rand(1), rand(1)) for i=1:100])

```

where the value of `MyInnerStruct.boolean_flag` is not known in advance. Then I want to do some calculations on all the elements of `MyOuterStruct.vector`

```Julia
function dosomething!(s::MyInnerStruct)
    if s.boolean_flag
        s.vector .+= 2.0*s.index
    else
        s.vector .-= 1.0
    end
end

dosomething!.(mystruct.vector)

```

depending on the boolean flag I’m switching implementation.

I thought I can exploit the type system to simplify this by defining differently my structs

```Julia
mutable struct MyInnerStructTrue
    index :: Int64
    vector :: Vector{Float64}
    a :: Float64
    b :: Float64
    c :: Float64
end

mutable struct MyInnerStructFalse
    vector :: Vector{Float64}
    a :: Float64
    b :: Float64
    c :: Float64
end

struct MyOuterStructUnion
    name :: String
    vector :: Vector{Union{MyInnerStructTrue, MyInnerStructFalse}}
end

dosomething!(s::MyInnerStructTrue) = s.vector .+= 2.0*s.index
dosomething!(s::MyInnerStructFalse) = s.vector .-= 1.0

```

and use it as

```Julia
innerstructs = []
for i=1:100
    b = rand(Bool)
    if b
        push!(innerstructs, MyInnerStructTrue(i, rand(100), rand(1), rand(1), rand(1)))
    else
        push!(innerstructs, MyInnerStructFalse(rand(100), rand(1), rand(1), rand(1)))
    end
end
mystruct = MyOuterStructUnion("my_out_struct", innerstructs)
dosomething!.(mystruct.vector)

```

this works but now I have a new struct (`MyInnerStructFalse`) which is identical to the previous (`MyInnerStruct` → `MyInnerStructTrue`) one except for a single entry (`index`).

Is there a way of _merging_ types? Something along the lines of

```julia
mutable struct MyInnerStructTrue
    index :: Int64
    <- put here MyInnerStructFalse
end

mutable struct MyInnerStructFalse
    vector :: Vector{Float64}
    a :: Float64
    b :: Float64
    c :: Float64
end

```

---

<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:** [February 2, 2024, 3:27pm UTC](https://discourse.julialang.org/t/a-question-on-the-type-system/109624/3 "2024-02-02T15:27:37Z")

</div>

> [@alemelis](#):
>
> Is there a way of _merging_ types? Something along the lines of

Not really, no.

In your setting, I would recommend not using the type system for that; just say that `index == -1` corresponds to the `false` case and be done with it.

The type system thing is amazing if you need extendibility across modules / packages / authors, or if you need compiler specialization (e.g. StaticArrays). If you don’t need that, it’s mostly better to keep stuff simple.

You could try

```julia
struct MyInnerInner
    vector :: Vector{Float64}
    a :: Float64
    b :: Float64
    c :: Float64
end
mutable struct MyInnerFalse
inner::MyInnerInner
end
mutable struct MyInnerTrue
index::Int64
inner::MyInnerInner
end

```

but I don’t recommend it.

It does give identical data layout to what you want, though:

```julia
julia> struct MyInnerInner
           vector :: Vector{Float64}
           a :: Float64
           b :: Float64
           c :: Float64
       end

julia> mutable struct MyInnerFalse
       inner::MyInnerInner
       end

julia> mutable struct MyInnerTrue
       index::Int64
       inner::MyInnerInner
       end

julia> sizeof(MyInnerInner)
32

julia> sizeof(MyInnerFalse)
32

julia> sizeof(MyInnerTrue)
40

```

---

<div class="post-metadata">

**Author:** ![alemelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alemelis/32/208433_2.png) [@alemelis](https://discourse.julialang.org/u/alemelis)\
**Post date:** [February 2, 2024, 4:44pm UTC](https://discourse.julialang.org/t/a-question-on-the-type-system/109624/4 "2024-02-02T16:44:34Z")

</div>

thanks @foobar_lv2  
I was wondering whether this wasn’t necessary at all
