JuliaArrays / StaticArrays.jl

Statically sized arrays for Julia
Other
762 stars 148 forks source link

`similar` for `FieldVector` converting to `MVector`/`SizedVector` #1273

Open mattsignorelli opened 1 month ago

mattsignorelli commented 1 month ago

similar when called with a FieldVector should output an array type equal to the input type (with possibly different eltype if specified). However the current behavior does not do this. For immutable eltypes, it converts to an MVector:

julia> using StaticArrays

julia> mutable struct Quaternion{T <: Number} <: FieldVector{4, T}
         q0::T
         q1::T
         q2::T
         q3::T
       end

julia> q = Quaternion(1,2,3,4)
4-element Quaternion{Int64} with indices SOneTo(4):
 1
 2
 3
 4

julia> similar(q)  # This should be a `Quaternion{Int64}`
4-element MVector{4, Int64} with indices SOneTo(4):
 4472459824
          1
     563205
          1

And for mutable eltypes, a SizedVector:

julia> mutable struct MyNum{T} <: Number
       num::T
       end

julia> q = Quaternion{MyNum{Int64}}(MyNum(1),MyNum(2),MyNum(3),MyNum(4))
4-element Quaternion{MyNum{Int64}} with indices SOneTo(4):
 MyNum{Int64}(1)
 MyNum{Int64}(2)
 MyNum{Int64}(3)
 MyNum{Int64}(4)

julia> similar(q)
4-element SizedVector{4, MyNum{Int64}, Vector{MyNum{Int64}}} with indices SOneTo(4):
 #undef
 #undef
 #undef
 #undef