# How do I put a parametrized struct into another parametrized container?

**URL:** <https://discourse.julialang.org/t/how-do-i-put-a-parametrized-struct-into-another-parametrized-container/44537>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [August 8, 2020, 1:46am UTC](https://discourse.julialang.org/t/how-do-i-put-a-parametrized-struct-into-another-parametrized-container/44537 "2020-08-08T01:46:55Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 8, 2020, 10:03pm UTC](https://discourse.julialang.org/t/how-do-i-put-a-parametrized-struct-into-another-parametrized-container/44537/10 "2020-08-08T22:03:08Z")

</div>

I think the problem happens inside [this method](https://github.com/JuliaCollections/DataStructures.jl/blob/64cc58956c7ca2fb2c0600fb8431c62950250d34/src/heaps/mutable_binary_heap.jl#L205--L214). Specially, this line:

```julia
push!(nodes, MutableBinaryHeapNode(convert(T, v), i))

```

because what happens is basically the same as:

```julia
julia> struct Wrapper{T}; v :: T; end

julia> vec = Vector{Wrapper{Any}}()
0-element Array{Wrapper{Any},1}

julia> push!(vec, Wrapper(convert(Any, 10)))
ERROR: MethodError: Cannot `convert` an object of type 
  Wrapper{Int64} to an object of type 
  Wrapper{Any}
...

```

In other words, the `push!` wraps your value inside a wrapper struct (`MutableBinaryHeapNode` more specifically), the code expects that converting your value to the type of the heap (i.e., `Any` or `SEvents`) they will create a `MutableBinaryHeapNode{Any}` (or `...{SEvents}`) and then push it into a vector. However, `typeof(convert(Any, s_event_obj))` is `typeof(s_event_obj)` not `Any`, and therefore the object created is a `MutableBinaryHeapNode{typeof(s_event_obj)}` and not a `MutableBinaryHeapNode{Any}` and cannot be pushed into a `Vector{MutableBinaryHeapNode{Any}}` because it is the wrong type.

I see two workarounds (i.e., not considering a fix in their side):

1. You need to use a concrete type instead of an abstract type. What would need you to STOP using a type that is different for each function inside it (i.e., not parametrized by closure). You would need to do:

```julia
struct SEvent
    callback::Function # or Any
    time::Float64
end

```

1. (shameless self-promotion) Use my implementation of a `MutableBinaryHeap`, that I prefer to `DataStructures.jl` implementation. It is available in [TrackingHeaps.jl](https://github.com/henriquebecker91/TrackingHeaps.jl) (registered package, you can just `add` it). It is fully documented, and have come up recently [in this thread](https://discourse.julialang.org/t/priority-queue-choice/42783).

---

_[View the full topic](https://discourse.julialang.org/t/how-do-i-put-a-parametrized-struct-into-another-parametrized-container/44537)._
