Modifying object dict keys

This is one of the ways to solve it, but it seems that, in his case, he ends up mutating the array used for the key, and does not want the mutated array to be able to recover the stored value, but instead any array that compared to equality to the key vector in its original state.

My suggestion would be something like:

function my_assign(d, v, k)
    if haskey(d, k)
        d[k] = v
    else
        d[deepcopy(k)] = v
    end
end

You probably can create a Dict wrapper that has this behavior. I think it is the safest, while not the most performant. The most performant alternative would be code carefully to never mutate keys, or only do so when you do not care about accessing that position in the Dict anymore.

3 Likes