# Why typeintersect(Int8,Int16) == Union{}

**URL:** <https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 9, 2021, 2:41am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053 "2021-08-09T02:41:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 9, 2021, 2:41am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/1 "2021-08-09T02:41:53Z")

</div>

Doc:

> While programs manipulate individual values, a type refers to a set of values.

By this, we can say that values come first, then types are created by grouping values. If so, `typeintersect(Int8,Int16)` should be `Int8`. Isn’t it?

---

<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:** [August 9, 2021, 3:07am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/2 "2021-08-09T03:07:57Z")

</div>

No. The key is that Int8(5) is not the same value as Int16(5). They are equal to each other, but have different memory layout, call different functions and have different behavior.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 9, 2021, 3:29am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/3 "2021-08-09T03:29:35Z")

</div>

IMHO, “memory layout” is not part of a value.

PS, Pascal’s definition of UInt8 (Byte): just a collection of integers.

```nohighlight
type
  Byte = 0..255; // from help of Lazarus/FPC

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 9, 2021, 4:56am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/4 "2021-08-09T04:56:31Z")

</div>

To determine whether two values are identical, you have to use `===`. Values that correspond to two different concrete types cannot be identical. For example,

```julia
struct Foo
    x::Int
end

struct Bar
    x::Int
end

```

```julia
julia> Foo(5) === Bar(5)
false

```

The same logic applies to `Int8` and `Int16`:

```julia
julia> Int8(5) === Int16(5)
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:** [August 9, 2021, 9:33am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/5 "2021-08-09T09:33:38Z")

</div>

The reason memory layout is part of value is pragmatic. If I have 2 different values that take up different amounts of memory, a slot for one won’t be able to hold the other.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 9, 2021, 10:25am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/6 "2021-08-09T10:25:33Z")

</div>

> [@Judd](#):
>
> Doc:
> 
> > While programs manipulate individual values, a type refers to a set of values.

Basically, I would say this part of the doc is a simplification.  
Good for someone first starting, but not so good once you start asking the hard questions like this.  
Because it doesn’t refer to what particular reasons things might be in that set.  
And you might think (reasonably) that two sets that are equal in value of elements would be the same type.

A more nuanced version of what that sentence is getting at can be found in Jeff’s thesis:

> Our goal is to design a type system for describing method applicability, and (similarly) for  
> describing classes of values for which to specialize code. Set-theoretic types are a natural  
> basis for such a system. A set-theoretic type is a symbolic expression that denotes a set of  
> values. **In our case, these correspond to the sets of values methods are intended to apply  
> to, or the sets of values supported by compiler-generated method specializations.**

(emphasis mine)  
Section 4.2 Type system (page 60)  
[Bezanson, J.W., 2015. _Abstraction in technical computing_ (Doctoral dissertation, Massachusetts Institute of Technology).](https://github.com/JeffBezanson/phdthesis/blob/master/main.pdf)

* * *

So we can now say that `typeintersect(Int8,Int16)` should indeed be `Union{}`,  
because there is no set of values that dispatch or specialize in the same way as both `Int8` and `Int16`.

---

<div class="post-metadata">

**Author:** ![Judd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/judd/32/27866_2.png) [@Judd](https://discourse.julialang.org/u/Judd)\
**Post date:** [August 9, 2021, 10:40am UTC](https://discourse.julialang.org/t/why-typeintersect-int8-int16-union/66053/7 "2021-08-09T10:40:47Z")

</div>

Thanks.

Now I can study the lattice constructed on types.
