Vector of dictionaries of different types in julia 0.6

I am a bit confused by the new syntax wherein julia 0.6. I have something like this:

a=Dict(["a"=>"b"])
b=Dict(["a"=>3])
c=Dict(["a"=>"c"])

I need a function that gets a dictionary vector without the need for an explicit conversion. I tried:

function bbb(a::Vector{Dict{String, Any}})
     println(a)
end

And it didn’t work.

Then i tried with

function bbb(a::Vector{Dict{String, T} where T})
     println(a)
end
bbb([a,b])   #Works
bbb([a,c])   #Fails
bbb([a,b,c]) #Works

I overloaded bbb with every combination I can get to make an explicit conversion. But I'm still wondering how to do it right.

+4
source share
1 answer

This is invariance in action. This is a difficult case, because there are two levels of parameterization, but the principle is the same.

  • Dict{String, Any} , , - Any. , Dict{String, Int} Dict{String, Any}.
  • Dict{String, T} where T . var T , Any Int.

, , :

  • Vector{Dict{String, T} where T} , Dict{String, T} where T. , Vector{Dict{String, Int}} Vector{Dict{String, T} where T}.
  • Vector{D} where D <: (Dict{String, T} where T) , . var D , , Dict{String, T} where T Dict{String, Int}.

You can express it a lot easier with shorthand notation:

function bbb(a::Vector{<: Dict{String, <: Any}})
     println(a)
end
+10
source

Source: https://habr.com/ru/post/1681888/


All Articles