StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Error when using `map` on `FieldVector` of `Vector`

Open mattsignorelli opened this issue 1 year ago • 1 comments

Consider the struct

struct Coords{T} <: FieldVector{3, T}
  x::Vector{T}
  y::Vector{T}
  z::Vector{T}
end

This is a "structure of arrays" layout of memory. For example we would store two coordinates (1,2,3) and (4,5,6) as

julia> c = Coords([1,4],[2,5],[3,6])
3-element Coords{Int64} with indices SOneTo(3):
 [1, 4]
 [2, 5]
 [3, 6]

Now say we'd like to get the first coordinate alone, as a vector/Svector. We would do:

julia> map(t->t[1], c)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Vector{Int64}

Closest candidates are:
  convert(::Type{Array{T, N}}, ::SizedArray{S, T, N, N, Array{T, N}}) where {S, T, N}
   @ StaticArrays ~/.julia/packages/StaticArrays/MSJcA/src/SizedArray.jl:88
  convert(::Type{Array{T, N}}, ::SizedArray{S, T, N, M, TData} where {M, TData<:AbstractArray{T, M}}) where {T, S, N}
   @ StaticArrays ~/.julia/packages/StaticArrays/MSJcA/src/SizedArray.jl:82
  convert(::Type{T}, ::AbstractArray) where T<:Array
   @ Base array.jl:665
  ...

Throws an error. However, this works completely fine for regular vectors:

julia> map(t->t[1], [c...])
3-element Vector{Int64}:
 1
 2
 3

mattsignorelli avatar Sep 16 '24 14:09 mattsignorelli

Shouldn't the supertype be Fieldvector{3, Vector{T}}? Don't know whether that makes a difference.

martinholters avatar Sep 17 '24 16:09 martinholters