Reputation: 861
I'm using Julia 1.5.2 on Win10
version 1: This one compiles. It actually works if all values of the Dict are of type 'V'. But that is not exactly what I want.
struct MyData{K,V}
data::AbstractVector{<:AbstractDict{<:K,V}}
end
version 2: This is what I actually want, but the compiler throws an UndefVarError: V not defined
exception. The only different is {<:K,V}
vs {<:K,<:V}
struct MyData{K,V}
data::AbstractVector{<:AbstractDict{<:K,<:V}}
end
Update: How to reproduce
julia> struct MyStruct{K,V}
data::AbstractVector{<:AbstractDict{<:K,<:V}}
end
julia> ar = [Dict{String,Any}("a" => 1)]
1-element Array{Dict{String,Any},1}:
Dict("a" => 1)
julia> MyStruct(ar)
ERROR: UndefVarError: V not defined
Stacktrace:
[1] MyStruct(::Array{Dict{String,Any},1}) at .\REPL[1]:2
[2] top-level scope at REPL[3]:1
Any idea why that is and how to fix it?
thanks for your help
Upvotes: 2
Views: 150
Reputation: 42214
This should be:
struct MyStructNew{K,V}
data::AbstractVector{<:AbstractDict{K,V}}
end
That is is your parametric types need to be concrete:
Here is the test:
julia> MyStructNew(ar)
MyStructNew{String,Any}(Dict{String,Any}[Dict("a" => 1)])
julia> MyStructNew([Dict{Number, AbstractString}(3=>"a")])
MyStructNew{Number,AbstractString}(Dict{Number,AbstractString}[Dict(3 => "a")])
If you actually want the types of K
and V
to be abstract you can use your original struct
but need to provide those types so Julia knows how to construct your object:
julia> struct MyStruct{K,V}
data::AbstractVector{<:AbstractDict{<:K,<:V}}
end
julia> MyStruct{AbstractString,Any}(ar)
MyStruct{AbstractString,Any}(Dict{String,Any}[Dict("a" => 1)])
Upvotes: 1