# How to convert element type of a container?

**URL:** https://discourse.julialang.org/t/how-to-convert-element-type-of-a-container/20293
**Category:** General Usage
**Tags:** question, array, type, convert
**Created:** [January 30, 2019, 9:35pm UTC](https://discourse.julialang.org/t/how-to-convert-element-type-of-a-container/20293 "2019-01-30T21:35:15Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [January 30, 2019, 11:44pm UTC](https://discourse.julialang.org/t/how-to-convert-element-type-of-a-container/20293/4 "2019-01-30T23:44:09Z")

</div>

This is basically the same question you asked in [How to get the container type of a container?](https://discourse.julialang.org/t/how-to-get-the-container-type-of-a-container/20253). 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:

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

```

otherwise,

```julia
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

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-convert-element-type-of-a-container/20293)._
