Even though inds is a Vector{Int}, ivvec[1] is a Vector{Float64}. Once found, this bug is easy to fix. ivvec = Any[inds, vals] or ivvec = (inds, vals) both do the trick.
Another weird thing is that inds is copied, while vals is just referenced:
I don’t have the time to dive into the specific details here, but a general advise: vectors of vectors are rarely a good design pattern in Julia. You have Matrix, N-dimensional Array, StructArrays.jl, Tables.jl and many other data structures that are suitable for efficient lookups with nested buffers.
Typically, containers are referenced, but promotion breaks this behavior presumably because the values in the containers are not longer the same. The Union type should work as expected.
If your provide more details about your use case, someone might be able to extend juliohm’s advice.
I am really surprised about this. I would have thought for sure it would automatically be a Vector{Any}. Breaking of the reference seems particularly odd.
What’s the rationale for doing promotion here? Or rather, what’s the general rule for when promotion happens?
If they all have a common promotion type then they get converted to that type using convert and that type is the array’s eltype
A somewhat abstract advice is to use Vector for elements which are used for “the same thing”. In your particular example, it would be more tidy to use a Tuple:
Thanks for the info! I see a lot of people trying to “solve my problem,” when there’s a solution right there in the post. I was more just curious about why this happens and whether it’s a bug or a feature.
I guess each step makes sense as a rule,
automatic promotion in a vector
promoting a Vector{Int} into a Vector{Float64}
copying promoted vectors but referencing non-promoted vectors
not being able to use a Float64 as an index
even if taken together they lead to some unintuitive behavior.
I guess if there are Julian lessons here, they are