Dictionaries.jl
Dictionaries.jl copied to clipboard
Automatic conversion between dictionaries
Currently, Base.Dict allows
julia> convert(Dict{Int,Int}, Dict())
Dict{Int64, Int64}()
but this fails with Dictionary:
julia> convert(Dictionary{Int,Int}, Dictionary())
ERROR: MethodError: Cannot `convert` an object of type
Dictionary{Any,Any} to an object of type
Dictionary{Int64,Int64}
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:218
Dictionary{I, T}(::Any) where {I, T} at /home/belmant/.julia/packages/Dictionaries/khydh/src/Dictionary.jl:60
Dictionary{I, T}(::Any, ::Any) where {I, T} at /home/belmant/.julia/packages/Dictionaries/khydh/src/Dictionary.jl:108
Stacktrace:
[1] top-level scope
@ REPL[8]:1
Would it be possible to define conversion operators that would allow it? Is it a deliberate choice not to define them? It is useful for initializing structs that have dictionary fields, which currently require typing in Dictionary{K,V}() with K and V potentially long types.
Since I believe keys and values are stored in vectors, we could delegate the conversion to conversion between vector types.
Yes the convert behaviour should be somewhat like that for Dict and Array. Currently it’s just not implemented yet. Thanks for the reminder
Addressed on Dictionary via #67. We can keep this issue open for further work on adding conversion methods to other dictionaries.