I’m using StaticArrays, and I have a function like the following
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.