# 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:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [January 28, 2023, 9:59am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/21 "2023-01-28T09:59:20Z")

</div>

> [@Salmon](#):
>
> In Julia, I can simply call Linear Algebra functions on Float128 types from some other package and it simply works.

This is the reason I started learning Julia a few years ago : I needed complicated algorithms (particle swarm optimisation) to work on non-Float64 ,(in paticulare, MultiFloats and BigFloats). This was NOT possible in python or R without reimplementing a whole library. Few lines of Julia code…

---

<div class="post-metadata">

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

</div>

While all of the above examples are really cool (and Julia is amazing of course), I would also mention [some drawbacks of the type system](https://viralinstruction.com/posts/badjulia/#the_type_system_works_poorly). I think if you paint a fair picture people will trust your recommendations more.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 28, 2023, 4:14pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/23 "2023-01-28T16:14:35Z")

</div>

Was not aware of this, thank you so much

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [January 30, 2023, 5:10am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/24 "2023-01-30T05:10:07Z")

</div>

I think that discussion is very one-sided. Its fair to say that not everybody loves the subtyping in Julia but what appear to be downsides as presented in that article often turn out to be really useful in practise. Eg it can be very convenient to subtype an abstract type and then just implement the part of the specification you actually need. And what do you need? Just try it out and the errors will tell you what methods you are missing.

---

<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 30, 2023, 5:42am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/25 "2023-01-30T05:42:57Z")

</div>

I think that whatever view you take on subtyping design, the points raised in that blog article are at a level that I would not bring to an introductory presentation. I would at least go through [The type system works well](https://viralinstruction.com/posts/goodjulia/#the_type_system_works_well) in the follow-up post before that.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [January 30, 2023, 6:21am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/26 "2023-01-30T06:21:56Z")

</div>

For an example of “whimsical” but incredibly useful self-defined units, here’s one from a [paper](https://onlinelibrary.wiley.com/doi/10.1111/ele.13987) I published last year:

> <https://github.com/allicramer/EcologicalPatchiness/blob/5fc0bc4d33d0f0bb68a957cbfdaa32f676d6484c/consolidate_units.jl#L7>

It’s a big meta-analysis of many ecological interactions, and by defining our own units for all the predators and prey in addition to the usual physical units, we could easily be sure all the scaling equations were dimensionally consistent.

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [January 30, 2023, 8:51am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/27 "2023-01-30T08:51:30Z")

</div>

Yep, there are always pros and cons associated with any system. I suppose my broader point is to avoid saying “the type system is amazing, it can do no wrong”. Chances are someone will correctly point out that it has drawbacks, and this will reduce the impact of selling Julia. For example, the “try and see what happens” approach is annoying for large scale projects - it wastes a lot of time waiting for e.g. CI (especially since the precompile + first run step is so long in Julia).

@jd-foster Yep, I would strive for a balanced view. Be honest with the trade-offs and I think overall they will appreciate it.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [January 30, 2023, 8:57am UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/28 "2023-01-30T08:57:01Z")

</div>

Here’s an example that doesn’t make types explicit.

Define a function

```julia
f(x,y) = -(3*(1-x)^2 * exp(-(x^2) - (y+1)^2)
    - 6*(x/5 - x^3 - y^5) * exp(-x^2 - y^2)
    - 1/3 * exp(-(x+1)^2 - y^2))

```

Explore it with a plot

```julia
using Plots
plotly() # this backend makes nice 3d plots
x = range(-4, step=0.1, stop=4)
y = x
surface(x, y, (x,y)->f(x,y),c=:viridis)

```

Use multiple dispatch to make a form for a vector (implicit), to use for optimization.

```julia
function f(θ)
   x,y = θ
   f(x,y) 
end

```

Optimize:

```julia
results = Optim.optimize(f, θstart, LBFGS(), 

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [February 17, 2023, 8:17pm UTC](https://discourse.julialang.org/t/seeking-short-examples-to-sell-julias-type-system/93502/29 "2023-02-17T20:17:34Z")

</div>

Kind of related to rock-paper-scissors, I stole an idea from a blog post and implemented a Pokemon-based introduction to multiple dispatch: the notebook is here [HW1a - Pokémon](https://gdalle.github.io/JuliaComputationSolutions/hw1a_solutions.html)

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