# Seeking short examples to "sell" Julia's type system

**URL:** <https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502>\
**Category:** Teaching & Outreach\
**Tags:** type\
**Created:** [January 25, 2023, 12:04pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502 "2023-01-25T12:04:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [January 25, 2023, 12:04pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/1 "2023-01-25T12:04:07Z")

</div>

## Context

In a few days, I’m going to go try to sell (as in persuade, not monetarily) a short Julia course to some first year MSc AI students.

The majority of these students come from a comparatively stronger mathematical background. So far, they have been taught PyTorch and will use it to submit to the NeurIPS reproducibility challenge.

## Problem

One of the key strengths of Julia is its type system and particularly how it interacts with multiple dispatch. It’s not hard to sell a CS student that unique combination, but math-oriented students (in my experience) haven’t been introduced to the ideas that

- You don’t have to use Python, and you might actually enjoy programming more if you don’t.
- A type system can be incredibly useful, not only for correctness and error checking, but also for speed and modelling (mathematical) systems in general.
- (Bonus) Dependency and environment management doesn’t have to suck.

I think the best way to introduce Julia is by example, where each example introduces a problem and explores how Julia solves it. **Does anyone have a very short example of using the type system in a way that might awaken something in an unsuspecting scientist?**

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [January 25, 2023, 12:16pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/2 "2023-01-25T12:16:06Z")

</div>

Two quick things from the top of my head:

The DifferentialEquations.jl and Measurements.jl example from Stefan’s talk [JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski - YouTube](https://www.youtube.com/watch?v=kc9HwsxE1OY)

The Distributions.jl package that allows you to write distribution agnostic code.

---

<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:** [January 25, 2023, 12:32pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/3 "2023-01-25T12:32:44Z")

</div>

Cuda.jl : the type system allows for writing a generic code running fast on both CPU and GPU. ParallelStencil.jl demonstrate this too.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 25, 2023, 12:45pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/4 "2023-01-25T12:45:10Z")

</div>

I can’t go past Unitful and how you can propagate units with almost no friction, then giving an example of defining your own unit (maybe a whimsical one) and using it to immediately do a calculation.

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [January 25, 2023, 12:58pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/5 "2023-01-25T12:58:33Z")

</div>

Forward mode automatic differentiation:

[https://www.wias-berlin.de/people/fuhrmann/AdSciComp-WS2223/week3/#automatic\_differentiation\_and\_nonlinear\_system\_solution](https://www.wias-berlin.de/people/fuhrmann/AdSciComp-WS2223/week3/#automatic_differentiation_and_nonlinear_system_solution)

Got the idea from this thread:  
[https://julialang.zulipchat.com/#narrow/stream/225542-helpdesk/topic/Comparing.20julia.20and.20numpy/near/209135246](https://julialang.zulipchat.com/#narrow/stream/225542-helpdesk/topic/Comparing.20julia.20and.20numpy/near/209135246)

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [January 25, 2023, 1:04pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/6 "2023-01-25T13:04:41Z")

</div>

> [@Datseris](#):
>
> The DifferentialEquations.jl and Measurements.jl example from Stefan’s talk

> [@jd-foster](#):
>
> I can’t go past Unitful and how you can propagate units with almost no friction

Yes! These are both fantastic, canonical examples that can be demonstrated in a TikTok, let alone a technical sales pitch! Nice suggestions

> [@LaurentPlagne](#):
>
> Cuda.jl : the type system allows for writing a generic code running fast on both CPU and GPU.

I think this might go over a few heads but I might bring up this and KernelAbstractions.jl in the course itself. Thanks for the suggestion

> [@j-fu](#):
>
> Forward mode automatic differentiation

Beautiful. These kids won’t know what hit them! I think a great title to my slides would be _“Can your Python do this?”_.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [January 25, 2023, 1:19pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/7 "2023-01-25T13:19:00Z")

</div>

> [@jacobusmmsmit](#):
>
> > Forward mode automatic differentiation
> 
> Beautiful. These kids won’t know what hit them! I think a great title to my slides would be _“Can your Python do this?”_.

Huh, my reaction to this was that python ought to have an answer to this, but… not really, apparently. (of course, ML people will say that nobody cares about forward and that reverse is where the real magic is, and that isn’t wrong, but still…)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 25, 2023, 1:42pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/8 "2023-01-25T13:42:15Z")

</div>

This simple example of [Plots + MonteCarloMeasurements](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/134) by @jonniedie is a killer!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 25, 2023, 1:57pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/9 "2023-01-25T13:57:26Z")

</div>

Even “simpler” example, using Measurements.jl:

```julia
using Measurements
x = 2.0 ± 0.5 # \pm<TAB>
y = 5.0 ± 1.0
z = 7.5 ± 1.5
typeof(x) # Measurement{Float64}
(x + y) * z # 52.0 ± 13.0
(x + y) / z # 0.93 ± 0.24
exp(x) # 7.4 ± 3.7

```

and composing it with Unitful:

```julia
using Unitful
x = (2.0 ± 0.5)u"m/s"
y = (5.0 ± 1.0)u"s"
z = (7.5 ± 1.5)u"kg"
typeof(x) # Quantity{Measurement{Float64}, 𝐋 𝐓 ^-1
x + y # ERROR: not dimensionally compatible!
x * y # 10.0 ± 3.2 m
x * z # 15.0 ± 4.8 kg m s^-1

```

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [January 25, 2023, 2:40pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/10 "2023-01-25T14:40:49Z")

</div>

Off topic but quick comment of appreciation to Rafael’s seemingly wiki-like ability to provide an exact link to obscure pieces of the Julia manual and old Discourse posts 🙂

I’ve never seen MonteCarloMeasurements.jl before! Looks great.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 25, 2023, 2:42pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/11 "2023-01-25T14:42:04Z")

</div>

[https://indico.cern.ch/event/1074269/contributions/4539601/attachments/2317518/3945412/why-julia slides.pdf#page=22](https://indico.cern.ch/event/1074269/contributions/4539601/attachments/2317518/3945412/why-julia%20slides.pdf#page=22)

see page 22, writing a GPU compatible DenseLayer in 4 lines

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 25, 2023, 2:56pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/12 "2023-01-25T14:56:45Z")

</div>

One of the things that may well cause a strong impression in math-oriented people is the possibility of optimizing parameters of a simulation by automatically differentiating an objective function which is dependent on the simulation itself.

One example of this, a little bit artificial to make it simple, is here: [⚡ Pluto.jl ⚡](https://m3g.github.io/2021_FortranCon/) (it is somewhat slow to open, because of some large figure). There, in the “Planetary Motion” and “We can differentiate everything!” sections, I show a simulation of the earth trajectory which misses the period. Then, we can differentiate the end-point of of the simulation (after a year) as a function of the initial position of the earth, and optimize the distance of the earth to the sun to fix that small drift in the final position.

The example is somewhat artificial, but it is likely that the idea of being able to differentiate complete simulations and optimize and tune simulation parameters like that can be very appealing for people working with simulations or dynamic systems in general.

Of course all that depends on the type system, in such a way that if the simulation is written with generic types appropriately, automatic differentiation, error propagation, etc, can work all the way through it.

That is related to performing sensitivity analysis in simulations, which is also discussed here: [Shadowing Methods for Forward and Adjoint Sensitivity Analysis of Chaotic Systems | FS](https://frankschae.github.io/post/shadowing/)

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [January 25, 2023, 3:42pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/13 "2023-01-25T15:42:22Z")

</div>

Several good advanced topics are proposed above, but since these are first-year non-CS students I think you should spend most time on the basics, and maybe introduce more advanced examples towards the end. Explain the benefits of multiple dispatch compared to object-oriented single dispatch, and demonstrate that in many applications (especially math heavy ones) giving the first argument special status is awkward. Show how the type system means Julia can compile specialized methods for each function depending on how it’s called, and how this is fundamental for Julia’s amazing performance. And point out that types need not be explicitly declared to reap these benefits. Explain why Python needs Numpy and other C-based accelerations and mention the two-language problem. Maybe compare some clunky Numpy calculations to Julia’s more elegant formulations. Etc.

All these points have been covered very well in other Julia introductions you can find in this forum and elsewhere online, so be sure to look around before you reinvent the wheel.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [January 25, 2023, 4:40pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/14 "2023-01-25T16:40:40Z")

</div>

Personally, I think one of the coolest examples of multiple dispatch is in linear algebra and this is probably also bound to impress math students.  
You want the eigenvalues of a matrix in python?  
Sure thing, it’s `eigenvals(M) `. Oh, but what about a hermitian matrix? Well here the eigenvalues will be real and you can use a better algorithm so you should use `eigenvalsh` instead. What about an upper diagonal matrix? What about a 2x2 matrix where I can write down the solution directly without using any scheme? Or a 2x2 hermitian matrix?  
In Julia I simply write `eigen` and do not have to worry about anything. The only step is defining your matrix to be hermitian and a static matrix in the beginning ( **way** more intuitive if you ask me) and the compiler does the rest for you.  
It’s not just about the optimization being faster that way. Its also that I do not need to put insane amounts of thought into my code which abstractions and classes I should find and who should inherit what. I simply write down my functions and as time goes on I include more specialized versions thereof as I encounter more requirements that need special treatment.  
This fact that I can just start my programming projects and don’t have to scrap half my code just because I need a higher level of abstraction was definitely the reason why I was hooked almost immediately. Even further, I find libraries that others have written much easier to understand that way.

---

<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:** [January 25, 2023, 11:06pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/15 "2023-01-25T23:06:27Z")

</div>

+1 for forward-mode AD. This was the video that hooked me: [(2675) Automatic Differentiation in 10 minutes with Julia - YouTube](https://www.youtube.com/watch?v=vAp6nUMrKYg&t=1s)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 25, 2023, 11:33pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/16 "2023-01-25T23:33:22Z")

</div>

Not very science-oriented, but I think this a nice way to showcase multiple dispatch (disclaimer: I’m the author of this blog post): [Rock–paper–scissors game in less than 10 lines of code](https://giordano.github.io/blog/2017-11-03-rock-paper-scissors/). Good luck doing that so short _and_ extensible without multiple dispatch (adding a fourth shape post-fact — i.e. outside of a class, in class-based object-oriented programming — without multiple dispatch is non-trivial).

I said this isn’t very science-oriented, but if you think about it, this kind of 2-body interactions have actually many scientific applications (which is also the [example](https://en.wikipedia.org/wiki/Multiple_dispatch#Examples) brought in the Wikipedia article about multiple dispatch).

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 25, 2023, 11:34pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/17 "2023-01-25T23:34:14Z")

</div>

There are some good examples in this thread of the type system and multiple dispatch here:

> [@Is multiple dispatch the same as function overloading?](https://discourse.julialang.org/t/is-multiple-dispatch-the-same-as-function-overloading/4145/5):
>
> The main difference between multiple dispatch and function overloading (esp. as implemented in C++/other OOP langs.) is that overloaded functions still generally have an implicit receiver of fixed type, which constrains method dispatch to only those variants defined for the receiver. Julia, on the other hand, being more of a functional language, dispatches on all arguments. The classic way of demonstrating this is to imagine you are implementing a game of Asteroids™. As part of the implementati…

Also see this talk (now embedded):

[![](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5a2f02c81c376da005f8610ce9cdd4ad47cfdf4.jpeg "JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski") ](https://www.youtube.com/watch?v=kc9HwsxE1OY)

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [January 26, 2023, 2:40pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/18 "2023-01-26T14:40:20Z")

</div>

Thank you everyone for the suggestions. I think the one I’ll go with something similar to “autodiff in 10 minutes”.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 26, 2023, 5:31pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/19 "2023-01-26T17:31:23Z")

</div>

Sorry to say, but the example, as you describe it, is not a selling point for _multiple_ dispatch, because I you can do all of that with _single_ dispatch in any other language (that supports it), even without ‘abstractions’ or inheritance.

@giordano 's example is more convincing.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [January 26, 2023, 9:33pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/20 "2023-01-26T21:33:19Z")

</div>

Hm i see what you mean but I do think the multiple dispatch approach still comes through here. After all, what makes this so powerful is that there are also generic methods for very generic matrices, covering a wide range of use case way beyond what you can realistically achieve with overloading. There are other powerful libraries written for linear algebra but none of them seem to be as flexible as Julia’s while at the same time being easy to use and fast. In Julia, I can simply call Linear Algebra functions on Float128 types from some other package and it simply works.

[Next page](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502.md?page=2)
