# Union troubles

**URL:** <https://discourse.julialang.org/t/union-troubles/39340>\
**Category:** General Usage\
**Created:** [May 12, 2020, 11:16am UTC](https://discourse.julialang.org/t/union-troubles/39340 "2020-05-12T11:16:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maarten\_van\_damme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maarten_van_damme/32/6946_2.png) [@maarten\_van\_damme](https://discourse.julialang.org/u/maarten_van_damme)\
**Post date:** [May 12, 2020, 11:16am UTC](https://discourse.julialang.org/t/union-troubles/39340/1 "2020-05-12T11:16:38Z")

</div>

I want to define a type that is effectively a vector of a matrix of operators. However, if the operator has norm zero or is proportional to the identity then we can simplify the necessary operations =\> we want to store this info. I wanted to use Union{E,M} where {E\<: Number,M\<:Operator}. If an element e::E then the corresponding operator is simply e\*idenitty.

Now the following happens:

function initialize(x::Vector{Matrix{Union{E,M}}}) where {E\<:Number,M\<:Matrix}  
E == eltype(M) || throw(ArgumentError(“scalar type should match opp eltype E ≠ (eltype(M))”))  
end

initialize([Matrix{Union{Int64,Matrix{Int64}}}(fill(2,3,3))])

fails with

ERROR: ArgumentError: scalar type should match opp eltype Union{Int64, Array{Int64,2}} ≠ Int64

What am I missing?  
Is there a better design?

At the moment I’m just going with a 3d array instead of Vector{Matrix{}} but I’m a bit unhappy with this, as the vector in reality is only periodic in it’s first argument, and I’m not really use a vector but some periodic vector type.

---

<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:** [May 12, 2020, 12:24pm UTC](https://discourse.julialang.org/t/union-troubles/39340/2 "2020-05-12T12:24:11Z")

</div>

I think if you change the `where` to

```julia
where {E<:Number, M<:Matrix{E}}

```

then you don’t even need the type check within the body.

---

<div class="post-metadata">

**Author:** ![maarten\_van\_damme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maarten_van_damme/32/6946_2.png) [@maarten\_van\_damme](https://discourse.julialang.org/u/maarten_van_damme)\
**Post date:** [May 12, 2020, 12:31pm UTC](https://discourse.julialang.org/t/union-troubles/39340/3 "2020-05-12T12:31:55Z")

</div>

Fair enough, but I still don’t understand why it errors out. After all, the error message implies that E is Union{Int64, Array{Int64,2}} while the function signature specifies that E has to subtype number.

---

<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:** [May 12, 2020, 2:09pm UTC](https://discourse.julialang.org/t/union-troubles/39340/4 "2020-05-12T14:09:35Z")

</div>

You are correct, this also intrigues me. I would write code with the assumption that `E <: Number` as it is enforced by the `where` clause of your function. However, showing `E` gives:

```julia
E = Union{Int64, Array{Int64,2}}
E <: Number = false

```

Would it be a bug in Julia, or it is just that `where` clauses do not work as we assume they work?

---

<div class="post-metadata">

**Author:** ![maarten\_van\_damme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maarten_van_damme/32/6946_2.png) [@maarten\_van\_damme](https://discourse.julialang.org/u/maarten_van_damme)\
**Post date:** [May 12, 2020, 2:13pm UTC](https://discourse.julialang.org/t/union-troubles/39340/5 "2020-05-12T14:13:24Z")

</div>

I also think it’s a bug. I can’t really use the workaround given here because my “operators” are abstract tensor maps from a different package, for which the element type is not defined… Shall I open a bug report on github?

---

<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:** [May 12, 2020, 2:22pm UTC](https://discourse.julialang.org/t/union-troubles/39340/6 "2020-05-12T14:22:39Z")

</div>

I think the ideal would be first catching the attention of a JuMP developer here in Discourse, but if this does not happen, these are [the guidelines for submitting a bug report to Julia](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#how-to-file-a-bug-report).

---

<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:** [May 12, 2020, 3:49pm UTC](https://discourse.julialang.org/t/union-troubles/39340/7 "2020-05-12T15:49:31Z")

</div>

As a workaround, maybe `E>:eltype(M)` would work?

That does look like a bug, as two method signatures that Julia sees as identical work differently:

```julia
julia> foo(x::T) where {E<:Number, M<:Matrix, T<:Matrix{Union{E,M}}} = E, M
foo (generic function with 1 method)

julia> foo(Matrix{Union{Int64, Matrix{Int64}}}(fill(2,3,3)))
(Union{Int64, Array{Int64,2}}, Array{Int64,2})

julia> foo(x::Matrix{Union{E,M}}) where {E<:Number, M<:Matrix} = E, M
foo (generic function with 1 method)

julia> foo(Matrix{Union{Int64, Matrix{Int64}}}(fill(2,3,3)))
(Int64, Array{Int64,2})

```

---

<div class="post-metadata">

**Author:** ![maarten\_van\_damme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maarten_van_damme/32/6946_2.png) [@maarten\_van\_damme](https://discourse.julialang.org/u/maarten_van_damme)\
**Post date:** [May 12, 2020, 4:50pm UTC](https://discourse.julialang.org/t/union-troubles/39340/8 "2020-05-12T16:50:08Z")

</div>

I just pushed the code with less type specifications (enough to pass unit tests and work for my use cases) and I’m now getting segfaults on other versions of julia … Bug report is open [where clause fails with union · Issue #35853 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/35853)
