How to convert element type of a container?

This is basically the same question you asked in How to get the container type of a container?. You’ll have to define a similar_type overload for each concrete FieldVector subtype you define. If that’s too much of a hassle, you could write a macro to generate the similar_type method along with the FieldVector subtype definition.

Note by the way that you’ll want to be more specific with the Size argument:

StaticArrays.similar_type(::Type{<:AA}, ::Type{T}, ::Size{(2,)}) where {T} = AA{T}

otherwise,

julia> aa = AA(1, 2)
2-element AA{Int64}:
 1
 2

julia> [aa; aa]
ERROR: DimensionMismatch("No precise constructor for AA{Int64} found. Length of input was 4.")
Stacktrace:
 [1] AA{Int64}(::Tuple{Tuple{Tuple{NTuple{4,Int64}}}}) at /Users/tkoolen/.julia/packages/StaticArrays/mcf7t/src/convert.jl:1
 [2] Type at /Users/tkoolen/.julia/packages/StaticArrays/mcf7t/src/convert.jl:4 [inlined] (repeats 3 times)
 [3] macro expansion at /Users/tkoolen/.julia/packages/StaticArrays/mcf7t/src/linalg.jl:114 [inlined]
 [4] _vcat at /Users/tkoolen/.julia/packages/StaticArrays/mcf7t/src/linalg.jl:96 [inlined]
 [5] vcat(::AA{Int64}, ::AA{Int64}) at /Users/tkoolen/.julia/packages/StaticArrays/mcf7t/src/linalg.jl:92
 [6] top-level scope at none:0
1 Like