Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing a ComponentArray with an array of symbols #71

Closed
scheidan opened this issue Mar 22, 2021 · 1 comment
Closed

Indexing a ComponentArray with an array of symbols #71

scheidan opened this issue Mar 22, 2021 · 1 comment

Comments

@scheidan
Copy link
Contributor

Sorry for the clumpsy title, I hope the example makes the idea clearer:

ca = ComponentVector(a=1, b=2, c=3)

ca[[1,2]]                # works
ca[[1,2]] .= [-1, -2]    # works

# Similar, it would be nice to index multiple fields with an array of symbols
ca[[:a, :b]]                # doesn't work
ca[[:a, :b]] .= [-11, -22]  # doesn't work

# This works, but is not quite as elegant:
getindex.(Ref(p1), [:a, :b])
setindex!.(Ref(ca), [-11, -22], [:a, :b])
@jonniedie
Copy link
Owner

The problem with that is:

julia> ca = ComponentVector(a=3, b=[4,1], c=[20, 11])
ComponentVector{Int64}(a = 3, b = [4, 1], c = [20, 11)

julia> a = getdata(ca)
5-element Vector{Int64}:
  3
  4
  1
 20
 11

julia> a[[1, 2:3]]
ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::Ptr) where T<:Integer at pointer.jl:23
  convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:250
  convert(::Type{T}, ::AbstractChar) where T<:Number at char.jl:180
  ...

julia> a[[2:3, 4:5]]
ERROR: ArgumentError: invalid index: UnitRange{Int64}[2:3, 4:5] of type Vector{UnitRange{Int64}}

Since a symbolic index can represent getting any number of types of results in a ComponentArray (including matrices), it seems that we should follow the pattern of Base here and similarly return an error. I guess it might be possible to check that all symbolic indices only return single elements and let it work for that case, but we'd have to make sure that doesn't slow indexing down in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants