Dictionaries.jl icon indicating copy to clipboard operation
Dictionaries.jl copied to clipboard

Automatic conversion between dictionaries

Open serenity4 opened this issue 4 years ago • 2 comments
trafficstars

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.

serenity4 avatar Aug 26 '21 20:08 serenity4

Yes the convert behaviour should be somewhat like that for Dict and Array. Currently it’s just not implemented yet. Thanks for the reminder

andyferris avatar Aug 27 '21 00:08 andyferris

Addressed on Dictionary via #67. We can keep this issue open for further work on adding conversion methods to other dictionaries.

serenity4 avatar Sep 19 '21 12:09 serenity4