# Good source for a deepdive on the type system?

**URL:** <https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063>\
**Category:** New to Julia\
**Created:** [December 28, 2018, 4:14pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063 "2018-12-28T16:14:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [December 28, 2018, 4:14pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/1 "2018-12-28T16:14:54Z")

</div>

Hi. I understand the basics of types in Julia, but there are some things I don’t quite get, especially with relation to traits and generics and stuff.

For example, I know what this is:

```julia
struct Foo{T} where T <: Real end

```

But I don’t get the difference between these three things:

```julia
foo(thing::Vector{T}) where T <: Real = ...
foo(thing::Vector{T} where T <: Real) = ...
foo(thing::Vector{T where T <: Real}) = ...

```

or these two things:

```julia
foo(thing::Vector{T}) where {T} = ...
foo(thing::Vector{T}) where T = ...

```

but both are in the documentation.

I also don’t really get what can be inside the where clause. Like, you can do some kinds of tests about types, but something like this fails:

```julia
foo(thing::T) where IteratorSize(T) == HasLength() = ...

```

I guess I just don’t understand the semantics of `where` very well, except that lets you declare a type variable and make some assertions about it (but I don’t understand what those are.

I see that the way to do traits is like:

```julia
foo(iterable) = foo(IteratorSize(iterable), iterable)
foo(::HasLength, iter) = ...
foo(::SizeUnknown, iter) = ...

```

That seems. Hm… weird. Is there a way to implement a method for any generic iterable? It seems like there must be a way to dispatch based on the availability of certain methods.

Anyway, If anyone has answers to things, that’s good, but I’m really looking for a resource that sets it all out clearly. I can’t seem to find (or perhaps understand?) the answers to these questions from the official docs.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 28, 2018, 4:51pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/2 "2018-12-28T16:51:44Z")

</div>

FWIW, there are some JuliaCon talks on parts of this which I found very instructive:

**JuliaCon 2017 | The State of the Type System | Jeff Bezanson**

[![](https://global.discourse-cdn.com/julialang/original/3X/4/9/492c9aff3d96076d5a907e1e76178ed47a4e7de8.jpeg "JuliaCon 2017 | The State of the Type System | Jeff Bezanson") ](https://www.youtube.com/watch?v=Z2LtJUe1q8c)

**JuliaCon 2018 | Subtyping made friendly | Francesco Zappa Nardelli**

[![](https://global.discourse-cdn.com/julialang/original/3X/9/1/91e03278926423ca9169e8215cee56a72a343cea.jpeg "JuliaCon 2018 | Subtyping made friendly | Francesco Zappa Nardelli") ](https://www.youtube.com/watch?v=Y95fAipREHQ)

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 28, 2018, 5:00pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/3 "2018-12-28T17:00:33Z")

</div>

To get you started with something,

`foo(thing::Vector{T}) where T <: Real ` and `foo(thing::Vector{T} where T <: Real)` are, AFAIK, equivalent.

`foo(thing::Vector{T}) where T <: Real` makes `foo` accept any vector that contains real numbers of a single type, like `Vector{Int}` or `Vector{Float64}`. It won’t accept, for example, `Vector{Union{Int, Float64}}`. Every element in the container must be of the same type.

`foo(thing::Vector{T where T <: Real})` on the other hand would accept `Vector{Union{Int, Float64}}` as well. The element type can be different for each element of the container.

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [December 28, 2018, 9:45pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/4 "2018-12-28T21:45:54Z")

</div>

Thanks so much for the reply! I finally got through both videos. The first one was super helpful in explaining the syntax! The second one explained things I mostly understood from the docs, but in a more difficult way.

I don’t have any background in higher maths or computer science, but I do love programming with sets, so I kind of understand what set theory is about, even if I don’t understand things like ∈, ∀ and ∃–obviously I’ve looked them up now, but I don’t have any training with that kind of thing. I still don’t know what t′ (t prime?) means.

The more I learn about formal proofs, the more convinced I am that they are really useful when designing a programming language (and type systems in particular), but I still can’t read them at all. I’m thinking about writing some kind of post or article about my experience learning Julia as a humanist-turned-professional-programmer. I’m thinking about calling it “∈ is more readable than `in`, and other \*\*\*\* scientists say.”

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [December 29, 2018, 12:47am UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/5 "2018-12-29T00:47:46Z")

</div>

Also see [More about types · The Julia Language](https://docs.julialang.org/en/v1/devdocs/types/) if you haven’t found that already.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 29, 2018, 10:50am UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/6 "2018-12-29T10:50:57Z")

</div>

Very interesting videos !  
Thinking about Francesco’s presentation, I wonder if the presented compatibility subtype system written in C could be re-written in Julia (e.g. starting on the presented simulator)? At the end of the video Francesco’s replied to this question saying that it would be very slow. Would it be true once it is compiled ?

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [December 29, 2018, 4:13pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/7 "2018-12-29T16:13:52Z")

</div>

> [@LaurentPlagne](#):
>
> At the end of the video Francesco’s replied to this question saying that it would be very slow.

If I were to take a complete stab in the dark at this question, I would suspect that it’s less a matter of C vs. Julia (though C is almost always faster, of course), and more that his algorithm is slow. The algorithms which are most aesthetically appealing are seldom the fastest. He mentioned that there is at least one function that’s doing recursion on two explicit stacks. My guess is that Julia implementation with performance that approaches the current implementation isn’t going to be much prettier.

The question is, if someone tried to re-implement this in Julia with performance in mind, is it possible that the flexibility of the language could suggest a faster algorithm than what is there in C? There is a story about Facebook switching from Git to Mercurial internally because they have some kind of enormous repo that git was too slow with. Mercurial wasn’t faster to start out with, but it was easier for them to implement the changes they needed because it was written in Python. (note that Mercurial is still slower than Git for the normal usecase, though it is certainly fast enough)

I’m not sure how well that scenario maps to this one, but it’s something to think about.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 29, 2018, 11:41pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/8 "2018-12-29T23:41:26Z")

</div>

Interesting guess.  
The bootstrap issue may also be considered.  
I must confess that this question is quite far from my area of expertise (if such thing exists), but this was what I liked in the video: as a scientific software developer it is nice (and of little stake) to get some insight into the language implementation. I think that Julia have this dangerous property to gather people previously isolated in separate fields (scientists, mathematicians, matlab users, HPC specialist, and true computer scientists,…). Of course this may cause some friction too 😉

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [December 30, 2018, 12:09am UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/9 "2018-12-30T00:09:24Z")

</div>

> [@LaurentPlagne](#):
>
> I think that Julia have this dangerous property to gather people previously isolated in separate fields (scientists, mathematicians, matlab users, HPC specialist, and true computer scientists,…). Of course this may cause some friction too 😉

We’ll manage somehow. 😘

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [January 27, 2019, 11:27pm UTC](https://discourse.julialang.org/t/good-source-for-a-deepdive-on-the-type-system/19063/10 "2019-01-27T23:27:53Z")

</div>

There is also Julia Subtyping: a Rational Reconstruction

> **[Julia Subtyping: a Rational Reconstruction](https://fzn.fr/projects/lambdajulia/)**
>
> Francesco Zappa Nardelli

> **[paper.pdf](https://fzn.fr/projects/lambdajulia/paper.pdf)**
>
> 805.39 KB
