# Type unstability with container generation in JuMP

**URL:** <https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 2, 2021, 9:40am UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433 "2021-04-02T09:40:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [April 2, 2021, 9:40am UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433/1 "2021-04-02T09:40:06Z")

</div>

Hi,

```julia
julia> model = Model(Gurobi.Optimizer)

julia> @variable(model, a[x in 1:10; x < 5], Bin)
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 4 entries:

julia> @variable(model, b[x in 1:10; x > 15], Bin)
JuMP.Containers.SparseAxisArray{Any, 1, Tuple{Any}} with 0 entries

```

Here, I would expect the second example to return a `JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}}`  
The current behavior can cause type unstability if we don’t check the emptiness of the resulting container beforehand.

Is it the expected behaviour and are we expected to do the emptiness check by ourselves ?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 4, 2021, 10:30pm UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433/2 "2021-04-04T22:30:57Z")

</div>

If the container is empty, it cannot know the element type.

Does the `Any` actually matter? You can’t end up using `b`, so it shouldn’t impact anything…

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [April 6, 2021, 4:47pm UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433/3 "2021-04-06T16:47:46Z")

</div>

> [@odow](#):
>
> If the container is empty, it cannot know the element type.

I guess the type can be known by using eltype(1:10). I’m not aware of a construction allowed by JuMP where the type could not be inferred.

> [@odow](#):
>
> Does the `Any` actually matter? You can’t end up using `b` , so it shouldn’t impact anything…

I wrap my model in a structure with the variables and some other objects. Then I add constraints. My code for generating the constraints does generally not need a special case when a set of variables is empty. The type of the variables is required for type signature in the structure, to ensure type inference all the way down.

.

---

<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:** [April 6, 2021, 5:42pm UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433/4 "2021-04-06T17:42:13Z")

</div>

Can’t you check if this will be the case and then explicitly create an empty and correctly typed `JuMP.Containers.SparseAxisArray` to use in its place?

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [April 6, 2021, 5:55pm UTC](https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433/5 "2021-04-06T17:55:24Z")

</div>

Yes, this is a possible workaround, but from a design perspective, I think the proposed behaviour is a better one.
