# Convert or copy to StaticArrays.MArray

**URL:** <https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001>\
**Category:** General Usage\
**Created:** [November 10, 2017, 10:57pm UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001 "2017-11-10T22:57:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [November 10, 2017, 10:57pm UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001/1 "2017-11-10T22:57:45Z")

</div>

I’m using StaticArrays, and I have a function like the following

```julia
function (obj::SomeObject{D}, x::AbstractVector{Int}) where {D}
    x_mutable = MVector{D,Int}(x)
    # [... mutate `x_mutable` in some way, based on obj]
    return SVector(x_mutable)
end

```

Essentially, I need to copy `x` to an `MVector`, modify it, and then return an `SVector`. But if I happen to pass an `MVector` to this function, `x_mutable` is equivalent to `x` (i.e. `x === x_mutable`). But I really want a copy instead.

Is there a simple way to “convert” to an MVector, but to perform a copy if the object was already an MVector?

The obvious solution of `copy(MVector(x))` results in an unnecessary copy when `x` is not an MVector.

Thanks in advance!

EDIT: I realize that by using multiple dispatch I could easily create a `convert_or_copy` function myself. My real question is whether such a pattern already exists in StaticArrays or elsewhere.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 11, 2017, 8:31am UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001/2 "2017-11-11T08:31:50Z")

</div>

[https://github.com/JuliaLang/julia/issues/23107](https://github.com/JuliaLang/julia/issues/23107)

Conclusion `MVector` should always copy.

---

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [November 11, 2017, 5:40pm UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001/3 "2017-11-11T17:40:55Z")

</div>

Aha, it sounds like a bug should be filed against StaticArrays, then:

```julia
julia> using StaticArrays

julia> a = @MVector [1,2,3]
3-element MVector{3,Int64}:
 1
 2
 3

julia> MVector(a) === a
true

```

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 11, 2017, 7:19pm UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001/4 "2017-11-11T19:19:14Z")

</div>

As a work around, for now you you could define a second method that takes x::MVector and copies x.

---

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [November 11, 2017, 8:27pm UTC](https://discourse.julialang.org/t/convert-or-copy-to-staticarrays-marray/7001/5 "2017-11-11T20:27:54Z")

</div>

Issue filed: [https://github.com/JuliaArrays/StaticArrays.jl/issues/335](https://github.com/JuliaArrays/StaticArrays.jl/issues/335)
