julia> typeof(setindex!!(OrderedDict{Union{},Union{}}(), 0, 4))
Dict{Int64, Int64}
This only happens if the key or element type needs widening. Not sure what the solution here is. Maybe OrderedCollections needs a BangBang extension?
Later...
Just tried to understand this a bit better, and the culprit method is this, which reads
function _setindex(d0::AbstractDict, v, k)
K = promote_type(keytype(d0), typeof(k))
V = promote_type(valtype(d0), typeof(v))
d = Dict{K, V}()
copy!(d, d0)
d[k] = v
return d
end
This feels quite funny to me: Any AbstractDict, under non-mutating _setindex, is just straight-up changed to a Dict.
What's the interface that every AbstractDict type must implement? Is it documented somewhere? I can't find it.
If it includes merge, and it might, then we could do
function _setindex(d0::AbstractDict, v, k)
return merge(d0, basetypeof(d0)(k => v))
end
where basetypeof gets the type of d0 stripped of type parameters (I forget now how to do it).
Or we could do something like
function _setindex(d0::AbstractDict, v, k)
K = promote_type(keytype(d0), typeof(k))
V = promote_type(valtype(d0), typeof(v))
d = basetypeof(d0){K,V}(k => v)
for k0, v0 in items(d0)
d[k0] = v0
end
return d
end
although I'm not sure what basetypeof(d0){K,V} there means, because even though every AbstractDict type must have a K and V type parameter, they don't need to be the first two type parameters.
This only happens if the key or element type needs widening. Not sure what the solution here is. Maybe OrderedCollections needs a BangBang extension?
Later...
Just tried to understand this a bit better, and the culprit method is this, which reads
This feels quite funny to me: Any
AbstractDict, under non-mutating_setindex, is just straight-up changed to aDict.What's the interface that every
AbstractDicttype must implement? Is it documented somewhere? I can't find it.If it includes
merge, and it might, then we could dowhere
basetypeofgets the type ofd0stripped of type parameters (I forget now how to do it).Or we could do something like
although I'm not sure what
basetypeof(d0){K,V}there means, because even though everyAbstractDicttype must have aKandVtype parameter, they don't need to be the first two type parameters.