Generic Structs in Dicts

Hi all :slight_smile:

Does anyone know why the following doesn’t work?

julia> struct A{T} a::T end

julia> Dict{<:A,Int}
Dict{<:A, Int64}

julia> Dict{<:A,Int}()
ERROR: MethodError: no method matching (Dict{<:A, Int64})()
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

Cheers

Looks like it needs an actual type

julia> Dict{<:Number, Int}()
ERROR: MethodError: no method matching (Dict{var"#s1", Int64} where var"#s1"<:Number)()

julia> Dict{Number, Int}()
Dict{Number, Int64}()

The shorthand <:A in

Dict{<:A,Int}()

translates to

(Dict{T,Int} where T<:A)()

which does not have a constructor defined as the error points out.

But what you are after anyway is

Dict{T where T<:A, Int}()

which works.

2 Likes