I want to create an array similar to some other array, which could be an Array, a MArray, or some other kind of mutable array-like container. But I would also like to set its dimensions to m×n. I’ve tried similar and it doesn’t seem to work. Consider the following:
using StaticArrays
m = n = 2
println(typeof(similar([1, 2, 3])))
println(typeof(similar([1, 2, 3], m, n)))
println(typeof(similar(@MVector([1, 2, 3]))))
println(typeof(similar(@MVector([1, 2, 3]), m, n)))
which outputs
Vector{Int64}
Matrix{Int64}
MVector{3, Int64}
Matrix{Int64}
I would have hoped
typeof(similar(@MVector([1, 2, 3]), m, n))
to output
MMatrix{2, 2, Int64, 4}
Is this expectation unreasonable? To get the behavior I want, the only way I have found for now is to test explicitly:
x = @MVector([1, 2, 3])
typeof(isa(x, MArray) ? MMatrix{m, n, eltype(x)}(undef) : similar(x, m, n))
which outputs what I expect
MMatrix{2, 2, Int64, 4}
Of course, that just works for MArrays. Is there a better way?