# Why do I get a type error when passing a dict with subtypes?

**URL:** <https://discourse.julialang.org/t/why-do-i-get-a-type-error-when-passing-a-dict-with-subtypes/50421>\
**Category:** New to Julia\
**Created:** [November 19, 2020, 10:16am UTC](https://discourse.julialang.org/t/why-do-i-get-a-type-error-when-passing-a-dict-with-subtypes/50421 "2020-11-19T10:16:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 19, 2020, 10:16am UTC](https://discourse.julialang.org/t/why-do-i-get-a-type-error-when-passing-a-dict-with-subtypes/50421/1 "2020-11-19T10:16:02Z")

</div>

Hi,

I’m passing a dict to a constructor containing objects which are a subtype of the type expected and I get a type error on it:

```julia
ERROR: TypeError: in keyword argument res_input, expected Dict{BluePrint,Int64}, got a value of type Dict{ConsumableBluePrint,Int64}

```

ConsumableBluePrint is a subtype of BluePrint though.

```julia
abstract type Entity end

abstract type BluePrint end

struct ConsumableBluePrint <: BluePrint
    type_id::UUID
    name::String
    ConsumableBluePrint(name::String) = new(uuid4(), name)
end

struct ToolBluePrint <: BluePrint
    type_id::UUID
    name::String
    lifecycle::Restorable
    ToolBluePrint(name::String, lifecycle::Restorable = Restorable()) =
        new(uuid4(), name, lifecycle)
end

struct ProducerBluePrint <: BluePrint
    type_id::UUID
    name::String
    lifecycle::Restorable
    res_input::Dict{BluePrint,Int64} # Required input per batch
    output::Dict{BluePrint,Int64} # Output per batch. The BluePrint and the number of items per BluePrint.
    max_batches::Int64
    ProducerBluePrint(
        name::String,
        lifecycle::Restorable = Restorable();
        res_input = Dict(),
        output = Dict(),
        max_batches::Int = 1,
    ) = new(uuid4(), name, lifecycle, res_input, output, max_batches)
end

```

So, why do I get this error? And how can I fix it?

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [November 19, 2020, 11:01am UTC](https://discourse.julialang.org/t/why-do-i-get-a-type-error-when-passing-a-dict-with-subtypes/50421/2 "2020-11-19T11:01:09Z")

</div>

Types in Julia are invariant wrt their parameters, more on that [in the docs](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types). That means, `Sub <: Super` does not imply `T{Sub} <: T{Super}`. If you need the concept of “`Super` and all it subtypes as a type parameter”, use `T{<:Super}` or `T{S where S<:Super}`

In your case, it seems that it’s better to work the other way: initialize your `res_input` and `output` with `Dict{BluePrint, Int64}()` or, if you use a comprehension, `Dict{BluePrint, Int64}(k => v for (k, v) in itr)`.
