JuliaArrays / JuliaArrays/StaticArrays.jl

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

Open
#1,276 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Julia REPL examples using map on the FieldVector{3,T} Coords structure, then compare them with map on [c...]. Start at the map behavior for FieldVector and inspect why the mapped Int64 values are converted to Vector{Int64}. Done means the FieldVector example returns the expected three values without a MethodError.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.