I am a bit confused about the new where syntax in julia 0.6.
I have something like this:
a=Dict(["a"=>"b"])
b=Dict(["a"=>3])
c=Dict(["a"=>"c"])
I want a function that receive a vector of dictionaries without having to make an explicit conversion. I tried with:
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 have overloaded bbb with every combination that I can recieve in order to make an explicit conversion. But I'm still wondering how is the proper way to do it.
This is invariance in action. It's a complicated case since there are two levels of parameterization, but the principle is the same.
Dict{String, Any} describes a dictionary where the keys are strings and the value type is exactly Any. Parametric invariance means that Dict{String, Int} is not a subtype of Dict{String, Any}.Dict{String, T} where T describes all dictionaries with string keys. The type var T can match any type, including Any or Int.Now, when you start talking about a vector of dictionaries, the same principle applies:
Vector{Dict{String, T} where T} describes a vector where the element type is exactly Dict{String, T} where T. Parametric invariance means that Vector{Dict{String, Int}} is not a subtype of Vector{Dict{String, T} where T}.Vector{D} where D <: (Dict{String, T} where T) describes all vectors where the elements are dictionaries with string keys. The type var D can match any dictionary type where the keys are strings, including Dict{String, T} where T or Dict{String, Int}.You can express this much more simply with the shorthand notation:
function bbb(a::Vector{<: Dict{String, <: Any}})
println(a)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With