Problems overloading setindex!()

Base.setindex!(entities::Entities, e::Array{Entity,1}, index::Blueprint) = (entities.entities[index] = e[1])

I find this a bit weird, as you dispatch on Array{Entity,1} just to only extract the first element. What happens if there are two elements in the vector? Why do you need this method in the first place if you only expect one element? Then at least I would do entities.entities[index] = only(e) so it errors if that assumption is violated.

The error comes from the fact that your entities dict stores objects of type Vector{Entity}, but you specifically instruct to extract the element from the Vector{Entity} that you have, which means you pass the wrong type. The other setindex! method also passes an Entity, and not a Vector of them.

I have no idea what the code is supposed to do, but maybe your issue goes away if you do:

Base.setindex!(entities::Entities, e::Array{Entity,1}, index::Blueprint) = (entities.entities[index] = e) # pass a vector
Base.setindex!(entities::Entities, e::Entity, index::Blueprint) = (entities.entities[index] = [e]) # pass a vector
2 Likes