andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
282 stars 28 forks source link

Automatic conversion between dictionaries #66

Open serenity4 opened 3 years ago

serenity4 commented 3 years ago

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.

andyferris commented 3 years ago

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

serenity4 commented 3 years ago

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