# Does Julia syntax do enough to encourage type stability?

**URL:** <https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219>\
**Category:** Internals & Design\
**Tags:** type-stability\
**Created:** [February 21, 2017, 5:30pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219 "2017-02-21T17:30:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [February 21, 2017, 5:30pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/1 "2017-02-21T17:30:07Z")

</div>

Having used Julia on a fairly regular basis for almost a year now, one of the most common causes I see of unnecessarily poor performance is type instability. It’s wonderful that Julia is dynamically typed for cases where that sort of thing is useful, but the fact that Julia _is_, after all, dynamically typed creates lots of opportunities for users to unknowingly create sub-optimal code. Having been brought up on mostly C++ and a little Fortran, I tend to be quite aware of the types of the objects in my code, but in the last few years I’ve been around an increasing number of people who are primarily familiar with Python both in the scientific and commercial communities. Some of these people tend not to think about types at all. (Indeed, I find Python’s approach of pretending types don’t exist at all to be a glaring absurdity in what is, for the most part, an otherwise fairly well-thought-out language.) I am also certainly guilty, even as a fairly experienced user, of occasionally writing type-unstable code where it doesn’t belong.

My question is this: do we think that the current Julia syntax does enough to encourage users to at least be aware of (if not enforce) the type stability (or instability) of their code?

To make this more concrete, let me make one not-very-serious suggestion that I haven’t put a huge amount of thought into. I love that we can now make type assertions on function output using, for example

```julia
function f(phi::AbstractFloat)::Complex{Float64}
    e^(im*phi)
end

```

but I wonder if it might be a good idea, to promote good habits, to _ **require** _ syntax like

```julia
function::Complex{Float64} f(phi::AbstractFloat)
   e^(im*phi)
end

```

so that, most functions as they are currently implemented would _require_ a `function::Any` or `f(x)::Any`. The only use this serves would be to force the user to think “I’m writing a function, but it’s return type isn’t controlled, so I better not put it in any performance critical code.” Of course my above example opens a can of worms with parametric types, so I’m certainly not claiming it is the best possible implementation of this type of idea. (On the other hand, I don’t think requiring type assertions on all input arguments is particularly useful, because of multiple dispatch.)

Some possibly negative side-effects of this would be that it might encourage less experienced users to write overly type-specific (and, possibly, as a result overly complicated) code. It would also likely be a big turn off to people coming from Python, a crowd which, in my experience, mostly doesn’t want to be bothered with this sort of thing at all regardless of its impact on performance or stability. Another problem that I haven’t thought much about is what are the consequences (if any) of most functions having implicit `convert` calls coming at their end (like in my examples above).

These are just random musings, but I wonder if the core developers of Julia have had similar thoughts. Has there been any thought given to this in the past? I’m skeptical of this myself, because I’m always skeptical of changes that don’t add any real functionality, but it’s something that’s occurred to me a number of times in the course of my using Julia. It would be nice if someone could come up with a better specific suggestion than the one I made here.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 21, 2017, 5:52pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/2 "2017-02-21T17:52:19Z")

</div>

> [@ExpandingMan](#):
>
> My question is this: do we think that the current Julia syntax does enough to encourage users to at least be aware of (if not enforce) the type stability (or instability) of their code?

Most “users” don’t need to be aware of it. If you’re calling functions in a machine learning, differential equation, etc. library which takes the vast majority of the time in your script, then type-stability in the outer function isn’t necessary to get good performance since the functions will always specialize on the types seen, and the cost of a single or a few dynamic dispatches is fairly minimal (in the 10’s of nanoseconds range).

Most functions should stay broad because you don’t know what you’ll get. Arguably, your example should be `Complex{T}` where you don’t know what the `T` is until you know the input type, and it could be hard to compute what that output type would have to be in general because that `T` could be some immutable defined by the user (though it would still be deducible and type-stable). So I would say that almost all uses of this kind of type assertions would be overly type-specific, and would encourage libraries not to use it.

Finding type instabilities is fairly easy with `@code_warntype`. And sure, you do have to learn a few things to properly write a performant library, but that’s not a problem Julia can solve: you do actually have to learn the language if you want to do something complex. There’s no way around that.

And I don’t think going even to something like warnings for type-unstable functions is a good idea either, becuase type-instabilities and non-strictly typed things are useful. Especially when prototyping, which is something Julia excels at.

Essentially, Julia runs the spectrum from Python to C/FORTRAN (with some offshoots of functional programming / Lisp). Julia doesn’t care what part of the spectrum you are programming at, and I think that’s the right way to go. Some people/projects live in the MATLAB/Python/R “use some scripts regime”, others live in the “library building” regime of C/FORTRAN. Julia does both just fine.

Proposals like this kind of force the user to think in more of a C/FORTRAN way, increasing the verbosity and “header information” to help make sure you get speed… but that’s exactly why I don’t use C/FORTRAN anymore.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [February 21, 2017, 6:02pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/3 "2017-02-21T18:02:11Z")

</div>

I think it’d be great to get better IDE support for detecting type instabilities. I imagine it wouldn’t be much harder than running `@code_warntype` on a function and tagging variables that are type-instable.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 21, 2017, 7:59pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/4 "2017-02-21T19:59:16Z")

</div>

That’s a great idea!

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [February 21, 2017, 8:45pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/5 "2017-02-21T20:45:38Z")

</div>

> [@quinnj](#):
>
> I think it’d be great to get better IDE support for detecting type instabilities. I imagine it wouldn’t be much harder than running @code\_warntype on a function and tagging variables that are type-instable.

Can you give an example how to use the @code\_warntype? I was thinking the same thing when I saw the initial post. Unfortunately, I didn’t get hang off the @code\_warntype yet. I think this could be an addition to linter-julia or separate package, but the basic functionality is a lot like linter.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 21, 2017, 9:37pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/6 "2017-02-21T21:37:00Z")

</div>

Have you seen this section of the manual?

[http://docs.julialang.org/en/stable/manual/performance-tips/#man-code-warntype](http://docs.julialang.org/en/stable/manual/performance-tips/#man-code-warntype)

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 21, 2017, 10:38pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/7 "2017-02-21T22:38:06Z")

</div>

> I think it’d be great to get better IDE support for detecting type instabilities. I imagine it wouldn’t be much harder than running @code\_warntype on a function and tagging variables that are type-instable.

I _love_ the idea, but would this really work for e.g. a function `f(x::Any)`? What types for `x` will you automatically probe the stability for?

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [February 21, 2017, 11:22pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/8 "2017-02-21T23:22:48Z")

</div>

I would just go off whatever `@code_warntype` says; it has it’s own internal algorithm for flagging variables. That way if there’s a false-positive, we submit an issue and everyone benefits from a better code\_warntype algo.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 21, 2017, 11:44pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/9 "2017-02-21T23:44:17Z")

</div>

I don’t understand… if you use `@code_warntype` with the natural signature, then even `identity` would be flagged as type unstable…

---

<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:** [February 22, 2017, 12:16am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/10 "2017-02-22T00:16:08Z")

</div>

> [@andyferris](#):
>
> I love the idea, but would this really work for e.g. a function f(x::Any)? What types for x will you automatically probe the stability for?

Possibly search the test cases til one if found that hits the function.  
If that is stable then continue to look for another, and so forth.  
Then in the GUI make a note of which test case revealed the type instability.

It is a bit complex, but not entirely insane.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [February 22, 2017, 12:19am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/11 "2017-02-22T00:19:57Z")

</div>

I don’t follow you here. Example?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 22, 2017, 12:38am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/12 "2017-02-22T00:38:06Z")

</div>

```julia
f(b, x) = b ? x + 1.0 : x

```

What arguments should the IDE pass to `code_warntype`? Does it try the entire product of combinations of all concrete subtypes of any? It’s type stable for `Tuple{Bool, Float64}`, but unstable for `Tuple{Bool, Int}`. What about this one:

```julia
g(b, x) = b ? x + one(eltype(x)) : x

```

It’s type stable for `g(true, rand(3))` but not for `g(true, view(rand(3,3), :, 1))`.

---

<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:** [February 22, 2017, 2:43am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/13 "2017-02-22T02:43:55Z")

</div>

So for

> `g(b, x) = b ? x + one(eltype(x)) : x`

I am suggesting that if the tests file contains:

```julia

@test length(g(false, rand(3,3)) == 9
@test length(g(true, rand(3,3)) == 9
@test length(g(true, view(rand(3,3), :, 1)) == 3)
@test length(g(false, view(rand(3,3), :, 1)) == 3)
@test length(g(true, sprand(3,3,0.5)) == 9)
@test length(g(false, sprand(3,3,0.5)) == 9)
@test length(g(true, view(sprand(3,3,0.5), :, 1)) == 3)
@test length(g(false, view(sprand(3,3,0.5), :, 1)) == 3)

```

Then the types tested by `code_warntype` should be, in order:

1. `Bool` and `Array{Float64,2}`
2. `Bool` and `SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}`
3. `Bool` and `SparseMatrixCSC{Float64,Int64}`
4. `Bool` and `SubArray{Float64,1,SparseMatrixCSC{Float64,Int64},Tuple{Colon,Int64},false}`

It could also do a more general search of types that show up in the package/tests, and do some kind of fuzzing-test-link combination of those that are allowed, randomly combined say 10 times with the chance given by the types frequency without repetition.

Alternatively, could use (SnoopCompile)[[GitHub - timholy/SnoopCompile.jl: Making packages work faster with more extensive precompilation](https://github.com/timholy/SnoopCompile.jl)] to generate the list of types to check.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [February 22, 2017, 7:23am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/14 "2017-02-22T07:23:13Z")

</div>

"I think it’d be great to get better IDE support for detecting type instabilities. "  
I like this idea. Great!

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [February 22, 2017, 7:42am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/15 "2017-02-22T07:42:34Z")

</div>

> [@oxinabox](#):
>
> It could also do a more general search of types that show up in the package/tests, and do some kind of fuzzing-test-link combination of those that are allowed, randomly combined say 10 times with the chance given by the types frequency without repetition.

I really like your idea. Currently I don’t have enough experience of code\_warntype. I can help establishing the server communication between Atom and julia. Here is the future version of julia-linter (which is just back end for linter): [https://github.com/TeroFrondelius/linter-julia/blob/JSON/lib/linter-julia.coffee](https://github.com/TeroFrondelius/linter-julia/blob/JSON/lib/linter-julia.coffee)

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [February 22, 2017, 7:48am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/16 "2017-02-22T07:48:07Z")

</div>

Lint can certainly do some of this, though at the moment to a limited extent. Using `@code_warntype` is fairly risky as it may require executing an arbitrary amount of other code first.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 22, 2017, 8:06am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/17 "2017-02-22T08:06:39Z")

</div>

Aside, in case someone reading this thread is not aware: there is the `Base.Test.@inferred` macro to help test that functions are type stable.

---

<div class="post-metadata">

**Author:** ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)\
**Post date:** [February 22, 2017, 11:37am UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/18 "2017-02-22T11:37:22Z")

</div>

> [@quinnj](#):
>
> I think it’d be great to get better IDE support for detecting type instabilities. I imagine it wouldn’t be much harder than running @code\_warntype on a function and tagging variables that are type-instable.

I agree this would be a great feature. The main tricky issue is how you map from lowered code back to source, but that’s a solvable problem and the pieces are already around in various places. The fact that we can be heuristic makes it easier, because we’re allowed to be wrong in unusual cases; e.g. there are multiple variables of the same name shadowing each other.

Doing it automatically, like a lint warning that you don’t have to think about, is harder for the reasons others have pointed out. But a common pattern is to define a function first and then test it with some example inputs. Juno can pick up a call to `foo(1, 2)` when you evaluate, then run the code\_typed in the background. Then we can show you variable types on hover in the editor, or alert you to any non-concrete types.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 22, 2017, 12:02pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/19 "2017-02-22T12:02:04Z")

</div>

That sounds potentially fantastic. Especially if it can be made relatively unobtrusive, so as not to annoy/deter the large and important group of users who just want to create some code without worrying about performance and type stability.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [February 22, 2017, 2:35pm UTC](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219/20 "2017-02-22T14:35:19Z")

</div>

I really like this idea, and it’s much better than anything I originally had in mind.

If it’s implemented this’ll give me some incentive to start using Atom, which I’m still frustrated with because I’m still unable to make it sufficiently “terminal-like” for my preferences. (Lucky for me there is the julia-vim package.)

[Next page](https://discourse.julialang.org/t/does-julia-syntax-do-enough-to-encourage-type-stability/2219.md?page=2)
