# Is there a static checking tool for Julia?

**URL:** <https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 24, 2019, 9:23am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271 "2019-03-24T09:23:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gu\_Junhua](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Gu\_Junhua](https://discourse.julialang.org/u/Gu_Junhua)\
**Post date:** [March 24, 2019, 9:23am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/1 "2019-03-24T09:23:56Z")

</div>

It is annoying that a program only report errors (e.g., no method matching …) when it reach the point of calling, especially for some time-consuming programs.

Since in Julia, we can give detailed type information, I wonder if there is any static type checking tool for Julia, which utilizes the type information to discover any possible error before running?

for example, If I write a function

```julia
function foo(x::AbstractArray)
x+1
end

```

which should actually be `x.+1` in the function’s content.

Can Julia discover this error (Julia alone or by using some external tool) in compiling stage?

Thanks

---

<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:** [March 24, 2019, 9:47am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/2 "2019-03-24T09:47:05Z")

</div>

Yes,

[https://github.com/ZacLN/StaticLint.jl](https://github.com/ZacLN/StaticLint.jl)  
but it has zero docs.

* * *

But note that julia is a dynamic language, so it is impossible to actually be 100% sure something is an error.

E.g. in your example i can have in my source code:

```julia
if rand() > 0.5
    @eval Base.:(+)(xs::AbstractArray, y::Int) = xs .+ y
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 25, 2019, 8:41am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/3 "2019-03-25T08:41:30Z")

</div>

> [@Gu\_Junhua](#):
>
> report errors (e.g., no method matching …) when it reach the point of calling, especially for some time-consuming programs.

An alternative solution is some unit testing (for building blocks, or the whole problem with a low-dimensional parametrization). Eg for a project I am working on now, the estimation with a 20^3 grid takes about 3 hours on the big server, but the “toy” parametrization 2^2 takes a few seconds on a laptop, so it goes in the unit tests.

---

<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:** [March 26, 2019, 10:00am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/4 "2019-03-26T10:00:52Z")

</div>

> [@Tamas\_Papp](#):
>
> An alternative solution is some unit testing

Both, one wants both.  
And defensive programming (the use of `@assert`)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 26, 2019, 10:06am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/5 "2019-03-26T10:06:17Z")

</div>

The more complex Julia systems I build, the less of a fan I am of overly defensive programming (except when working with external state, eg the network or a file system). I find that relying on dynamic (in practice, AOT for undefined methods) failure (“duck typing”) is sufficient, and _ex post_ it always turns out that I always over-constrain with assertions.

The exceptions are checking _values_ (as opposed to _types_), eg verifying that a matrix is PSD when required. But this is usually not something amenable to static analysis, and also it prompts me to redesign the API occasionally (eg accept Cholesky factors as the entry point, which are is trivial to check, and make other methods wrap this and fail there).

---

<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:** [March 26, 2019, 10:34am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/6 "2019-03-26T10:34:32Z")

</div>

Indeed values, not types.

I am not saying put in type constraints you are not going to be checking.  
I am big against that.

but a few `@assert length(a) == length(b)` can be helpful

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 26, 2019, 10:55am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/7 "2019-03-26T10:55:54Z")

</div>

> [@oxinabox](#):
>
> a few `@assert length(a) == length(b)` can be helpful

I agree with you about this, but nevertheless I find that Julia frequently allows me to design code in a way that these are not needed. Eg broadcasting and linear algebra operations will fail with a dimension mismatch, so if any of those are used it should be fine. I, of course, set up CI tests for catching these errors.

I am not saying that `@assert` should not be used, on the contrary, just that they are needed less in Julia than some other languages I have used. I find the API carefully engineered for this, eagerly throwing an error instead of doing something clever. Cf R’s

```R
> 1:5 + 1:2
[1] 2 4 4 6 6

```

which now throws a warning but that is easy to overlook.

---

<div class="post-metadata">

**Author:** ![tbonza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbonza/32/10667_2.png) [@tbonza](https://discourse.julialang.org/u/tbonza)\
**Post date:** [October 7, 2019, 2:33pm UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/8 "2019-10-07T14:33:57Z")

</div>

Any thoughts about using [JuliaLang/PackageCompiler.jl](https://github.com/JuliaLang/PackageCompiler.jl) as a substitute for a static type checker like [google/pytype](https://github.com/google/pytype)? I’m currently experimenting with this approach and am interested in hearing other perspectives/experiences. My use case is setting up data pipelines.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 7, 2019, 2:43pm UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/9 "2019-10-07T14:43:18Z")

</div>

But PackageCompiler doesn’t do any static type checking at all, so how are they comparable?

---

<div class="post-metadata">

**Author:** ![racinmat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/racinmat/32/11715_2.png) [@racinmat](https://discourse.julialang.org/u/racinmat)\
**Post date:** [December 6, 2019, 1:30am UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/10 "2019-12-06T01:30:47Z")

</div>

I am really looking foward to time when there will be static analyzer of Julia.  
I understand that with metaprogramming and modifications of AST it may be really difficult to infer the type correctly, but I think just the fact that Julia is dynamic language is not a no-go for static analyzers.

For example, for PHP, which is a profoundly dynamic language there now exists [https://github.com/phpstan/phpstan](https://github.com/phpstan/phpstan) which is able to deal with the dynamics using Union and Intersection types and type inference on them.  
And of course you can write

```julia
if rand() > 0.5
    @eval Base.:(+)(xs::AbstractArray, y::Int) = xs .+ y
end

```

and argue that static analyzer would not be able to deal with it, but in reality, how often you use randomly, or generally externally conditioned declaration of functions in such manner that you can not tell by static analysis how it will go?

Of course for dynamic language there will be something you need to give up when wanting static analysis, but I think people who want static analysis already try to write code that can be analysed and will aim for deterministic declaration of functions.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [February 10, 2022, 7:44pm UTC](https://discourse.julialang.org/t/is-there-a-static-checking-tool-for-julia/22271/11 "2022-02-10T19:44:01Z")

</div>

[https://github.com/aviatesk/JET.jl](https://github.com/aviatesk/JET.jl)
