Type aggregation

I have these structs:

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:

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:

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} ?

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

1 Like

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

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))