# Similar\_type for Vector

**URL:** https://discourse.julialang.org/t/similar-type-for-vector/58945
**Category:** New to Julia
**Tags:** staticarrays
**Created:** [April 9, 2021, 9:15pm UTC](https://discourse.julialang.org/t/similar-type-for-vector/58945 "2021-04-09T21:15:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![circonflexe](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@circonflexe](https://discourse.julialang.org/u/circonflexe)
#### Post date: [April 9, 2021, 9:15pm UTC](https://discourse.julialang.org/t/similar-type-for-vector/58945/1 "2021-04-09T21:15:28Z")

</div>

So I have a variable `v` which may take several types, including (at least) `Vector{T}`, `SVector{N,T}`, where `N` is an integer and `T` is a real type (including at least `Int`, `Rational{Int}` and `Float64`). I want to compute a type similar to `v` but with element type promoted to some other type `Q`.

In the case of `SVector`, the following works: `similar_type(v, Q)`. However, no method is implemented when `v` is a plain old `Vector`.

I could override this by defining `StaticArrays.similar_type(::Vector, T::Type) = Vector{T}` (or, even better, a more general method, although this one does what I want), but this although this feels like fixing something which should have been done, it is still technically type-piracy. Since this is too trivial to reasonably have been an oversight in `StaticArrays.jl`: is there a good reason why this package does not define `similar_type` methods for plain `Array` types?

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 10, 2021, 4:45am UTC](https://discourse.julialang.org/t/similar-type-for-vector/58945/2 "2021-04-10T04:45:17Z")

</div>

Isn’t this sort of what `similar(v, Q)` should do? Or do you require a stricter type match?

---

<div class="post-metadata">

### Author: ![circonflexe](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@circonflexe](https://discourse.julialang.org/u/circonflexe)
#### Post date: [April 10, 2021, 1:15pm UTC](https://discourse.julialang.org/t/similar-type-for-vector/58945/3 "2021-04-10T13:15:45Z")

</div>

`similar(v, Q)` returns a value; I want a type. Of course, I could call `typeof(similar(v,Q))` or even `first(Base.return_types(similar,Tuple{typeof(v),Type{Q}}))`, but the first solution would (probably) allocate an array, while the second is completely inelegant.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 10, 2021, 1:57pm UTC](https://discourse.julialang.org/t/similar-type-for-vector/58945/4 "2021-04-10T13:57:32Z")

</div>

The latter is certainly not the right answer as it’s not required to return the narrowest match
