# Why Unions dont inheret its union?

**URL:** https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166
**Category:** New to Julia
**Created:** [October 11, 2020, 3:14pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166 "2020-10-11T15:14:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 11, 2020, 3:14pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/1 "2020-10-11T15:14:55Z")

</div>

Why does this return false

```julia
IntOrString = Union{Int,AbstractString}
(IntOrString <: String, IntOrString <: Int)

```

I am not questioning the decision, just curious to know the reason behind it  
Also while we are at it why are unions not a DataType

```julia
Union <: DataType
DataType <: Union

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 11, 2020, 3:25pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/2 "2020-10-11T15:25:31Z")

</div>

> [@systems](#):
>
> I am not questioning the decision, just curious to know the reason behind it

Because, in your example, an `Int` is not a subtype of `String`. Think about it like sets. You have the set of all `Int` unioned with all `AbstractString`. This is clearly bigger than the set of `String`. In fact you have:

```julia
julia> String <: IntOrString
true

```

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 11, 2020, 3:41pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/3 "2020-10-11T15:41:14Z")

</div>

I think I might have figured it out  
Union are not treated as a DataType, because typeof(), can never return a Union

The type of a value can never be a Union

But now this make me think that

```julia
IntOrString <: String

```

should instead raise an error, not just return `false`

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 11, 2020, 3:44pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/4 "2020-10-11T15:44:47Z")

</div>

No. It is critical for Julia that `<:` return either `true` or `false` for every combination of types.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 11, 2020, 3:46pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/5 "2020-10-11T15:46:19Z")

</div>

> [@systems](#):
>
> Union are not treated as a DataType, because typeof(), can never return a Union

I’m not sure what you mean but note:

```julia
julia> abstract type Foo end

julia> Foo isa DataType
true

```

but `typeof` can never return `Foo`.

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 11, 2020, 3:58pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/6 "2020-10-11T15:58:23Z")

</div>

> [@kristoffer.carlsson](#):
>
> but `typeof` can never return `Foo`.

It can never return Foo, because Foo is an abstract type, that cannot be instantiated  
Only concrete type can be instantiated

---

<div class="post-metadata">

### Author: ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)
#### Post date: [October 11, 2020, 4:00pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/7 "2020-10-11T16:00:20Z")

</div>

Exactly, IntOrString is not a type or a DataType , its more like a function on Types, but is not itself a type , hence the raise an error make sense to me

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 11, 2020, 4:03pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/8 "2020-10-11T16:03:30Z")

</div>

> [@systems](#):
>
> It can never return Foo, because Foo is an abstract type, that cannot be instantiated  
> Only concrete type can be instantiated

Yes, I know, my point was that you said

> [@systems](#):
>
> Union are not treated as a DataType, because typeof(), can never return a Union

and I gave an example of where something is treated as a `DataType` even though `typeof` cannot return it.

---

<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: [October 11, 2020, 4:46pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/9 "2020-10-11T16:46:33Z")

</div>

Unions not being of type `DataType` has nothing to do with the fact that `IntOrString <: String == false`.

Let’s say you have a cage whose dweller has the type `const HamsterOrBird = Union{Hamster, AbstractBird}`. When you ask `HamsterOrBird <: Canary`, it means “if I know that there is a `HamsterOrBird` in the cage, can I be sure that the caged creature is a `Canary`?”

Maybe you meant the subtyping in the opposite direction? `IntOrString >: String` is true, meaning that if `x isa String` then `x isa IntOrString`.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 11, 2020, 6:01pm UTC](https://discourse.julialang.org/t/why-unions-dont-inheret-its-union/48166/10 "2020-10-11T18:01:27Z")

</div>

> [@Vasily\_Pisarev](#):
>
> Unions not being of type `DataType` has nothing to do with the fact that `IntOrString <: String == false` .

Also note that

```julia
julia> Union{Int,UInt} <: Integer
true

julia> Union{Int,UInt}
Union{Int64, UInt64}

julia> typeof(Union{Int,UInt})
Union

julia> typeof(Integer)
DataType

```

The `typeof` of a `Type` has little to do with the subtype relation.
