# Type aggregation

**URL:** <https://discourse.julialang.org/t/type-aggregation/90270>\
**Category:** New to Julia\
**Created:** [November 15, 2022, 2:48am UTC](https://discourse.julialang.org/t/type-aggregation/90270 "2022-11-15T02:48:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 15, 2022, 2:48am UTC](https://discourse.julialang.org/t/type-aggregation/90270/1 "2022-11-15T02:48:41Z")

</div>

I have these structs:

```julia
struct A{T}
    n::T
end

struct B{T,N}
    n::T
    m::N
end

C{K} = B{T,N} where {T,N<:A{K}} where {K}

a1 = A(1)
a2 = A(1.0);

```

Then I construct a `Vector`:

```julia
vec1 = [B(1, a1), B(1, a1)]

2-element Vector{C{Int64, Int64, A{Int64}}}:
 C{Int64, Int64, A{Int64}}(1, A{Int64}(1))
 C{Int64, Int64, A{Int64}}(1, A{Int64}(1))

```

As can be seen `vec1`’s element type is ` C{Int64, Int64, A{Int64}}`.

Then, I construct aother `Vector`:

```julia
vec2 = [B(1, a1), B(1, a2)]

2-element Vector{B{Int64}}:
 C{Int64, Int64, A{Int64}}(1, A{Int64}(1))
 C{Float64, Int64, A{Float64}}(1, A{Float64}(1.0))

```

There `vec2`’s element type is `B{Int64}`. But the all elements of `vec2` is `C`, so I thought the `vec2`’ elemnt type is `C`, and the type of `vec2` is `Vector{C}`

I thought there must be some order rules on type aggregating? The order thoughts `B{Int64}` is “more concrete” than `C`, so the result is `Vector{B{Int64}}` rather than `Vector{C}`?

Is there a way I can append a method, then whenever I run `vec2 = [B(1, a1), B(1, a2)]`, the result will automatically transformed to `Vector{C}` ?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 15, 2022, 3:03am UTC](https://discourse.julialang.org/t/type-aggregation/90270/2 "2022-11-15T03:03:14Z")

</div>

> [@Brian1](#):
>
> `vec2 = [B(1, a1), B(1, a2)]`

`vec2 = C[B(1, a1), B(1, a2)]`

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [November 15, 2022, 3:12am UTC](https://discourse.julialang.org/t/type-aggregation/90270/3 "2022-11-15T03:12:26Z")

</div>

Thanks for your reply. That works, but can not automatticlly work.  
I find another way to do it:

```julia
Base.promote_rule(::Type{T}, ::Type{N}) where {T<:C,N<:C} = C

[B(1, a1), B(1, a2)]
2-element Vector{C{K} where K}:
 C{Int64, Int64, A{Int64}}(1, A{Int64}(1))
 C{Float64, Int64, A{Float64}}(1, A{Float64}(1.0))

```
