# Strange behavior of union of types

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298>\
**Category:** New to Julia\
**Created:** [February 28, 2023, 1:18am UTC](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298 "2023-02-28T01:18:49Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 2, 2023, 8:18am UTC](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298/21 "2023-03-02T08:18:38Z")

</div>

> [@uniment](#):
>
> (and therefore, `T` cannot be an abstract type if it’s a `Tuple` type parameter)

> [@Sevi](#):
>
> Isn’t that a contradiction?

Sorry, maybe I’m not phrasing this right.

What I mean is, for a parent type `X=Union{Vector{T}, Tuple{T, T}} where T <: Number`, that `Union{Vector{Number}, Tuple{Int,Int}}` would _not_ be a subtype—that if the `Union` contains both `Vector` and `Tuple`, that their type parameters must be identical as in the child type `Union{Vector{Int}, Tuple{Int,Int}}`. If `Tuple` is not present in this union, however, then the match is looser: `Vector{Number}` should be a subtype of `X`. (as a parent type a larger union is easier to match, but as a child type a larger union is harder to match)

In this way, `X` would be a superset of `Vector{<:Number}`, a superset of `Tuple{T,T} where T<:Number`, and a superset of `Union{Vector{T}, Tuple{T,T}} where T<:Real`, and `X` would be a subset of `Union{Vector, Tuple{T,T}} where T<:Number` and a subset of `Union{Vector,Tuple}`.

Hopefully that makes sense.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [March 2, 2023, 8:44am UTC](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298/22 "2023-03-02T08:44:02Z")

</div>

EDIT: Changed `X` to `X1` and `X2` to clarify

Thanks for the explanation. I think I get what you are describing, but I think there is still a contradiction:

```julia
X1 = Union{Vector{T}, Tuple{T, T}} where T <: Number
X2 = Union{Vector{T}} where T <: Number

```

If I understood correctly, then

- `Vector{Number}` _should not_ be in `X1`
- `Vector{Number}` _should_ be in `X2`

That means your desired behavior

> [@uniment](#):
>
> `X` [meaning `X1`?] would be a superset of `Vector{<:Number}`

is not possible, since `Vector{Number} <: Vector{<:Number}` is true in all other contexts. To change that, we would need to change the meaning of `Vector{<:Number}` itself to allow only concrete types (like the tuple), but I don’t think this is what you implied?

And I think it would be really weird that we add an expression to a union, and it suddenly contains less types.

Hope I’m not missing the point and keep sounding like a broken record 😅

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 2, 2023, 9:00am UTC](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298/23 "2023-03-02T09:00:46Z")

</div>

It seems my explanation still leaves something to be desired😅

When I said if `Tuple` is not present in the union, then the match should be looser—I was referring to the union on the LHS, not the RHS.

Thus, `Vector{Number} <: X`, because the LHS is not unioned with a `Tuple`.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [March 2, 2023, 9:25am UTC](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298/24 "2023-03-02T09:25:18Z")

</div>

I’ve made an edit to my previous post to clarify what I am referring to with `X` and what I thought you are referring to.

Ok, then I misunderstood. You mean the LHS here?

```julia
Vector{Number} <: X
Tuple{Int,Int} <: X
Union{Vector{Number}, Tuple{Int,Int}} <: X

```

But now I’m lost… If `Vector{Number}` should be a subtype (read: subset) of `X` and also `Tuple{Int, Int}` should be a subtype of `X`, then their union should be as well, in my opinion. Otherwise I just wouldn’t call it “union” and make the whole thing work analogously to sets (but this is what the developers had in mind, according to Jeff’s talk linked above).

[Previous page](https://discourse.julialang.org/t/strange-behavior-of-union-of-types/95298.md?page=1)
