68 Stars
Updated Last
1 Year Ago
Started In
July 2013


Build Status Coverage Status

Type-based static analysis for the Julia programming language.

There are three main checks you can run: checkreturntypes, checklooptypes, and checkmethodcalls. Running a check on a function checks each method; running a check on a module checks each function (by checking each method of each function).

Installing This Package

To use any of these functions, you'll need to Pkg.add("TypeCheck") once to install the package on your computer and then import it using using TypeCheck. You'll need to re-import every time you restart the REPL.

checkreturntypes: do the return types of your functions depend on the types, not the values of your arguments?

It is considered good style in Julia to have the return type of functions depend only on their argument types, not on the argument values. This function tries to check that you did so.

You can run this on a generic function or on a module:

  • checkreturntypes(istext)
  • checkreturntypes(Base)

It is only effective at catching functions with annotated argument types.

It will catch things like:

julia> foo1(x::Int) = isprime(x) ? x: false
foo1 (generic function with 1 method)

julia> checkreturntypes(foo1)

However, it will not catch:

julia> foo2(x) = isprime(x) ? x : false
foo2 (generic function with 1 method)

julia> checkreturntypes(foo2)

Additionally, it does a check to see if the return type of the function depends on a function call in the return statement. This prevents the analysis from complaining about every function that calls a "bad" function. However, it's possible that this silences too many alerts.

checklooptypes: do the variables in your loops have stable types?

A common performance problem is having unstable (numeric) variable types in an important loop. Having stable types within loops allows Julia's JIT compiler to output code as fast as a static compiler; having unstable types means resorting to slower, dynamic code.

You can run this on a generic function or on a module:

  • checklooptypes(sort)
  • checklooptypes(Base)

It will complain about:

julia> function barr1()
         for i in 1:10
           x *= 2.5
barr1 (generic function with 1 method)

julia> checklooptypes(barr1)

It will correctly not complain about:

julia> function barr2()
         x = 4
         x = 2.5
         for i=1:10
           x *= 2.5
barr2 (generic function with 1 method)

julia> checklooptypes(barr2)


julia> function barr3()
         x::Int = 4
         for i=1:10
           x *= 2.5
barr3 (generic function with 1 method)

julia> checklooptypes(barr3)

(barr3() will throw an error rather than actually making x a Float64.)

It is possible that it misses loose types in some cases, but I am not currently aware of them. Please let me know if you find one.

checkmethodcalls: could your functions have run-time NoMethodErrors?

NoMethodErrors are probably the most common error in Julia. This is an attempt to find them statically.

You can run this on a generic function or on a module:

  • checkmethodcalls(sort)
  • checkmethodcalls(Base)

This functionality is still clearly imperfect. I'm working on refining it to be more useful.

More Helper Functions

This package also defined code_typed(f::Function) to get the Expr for each method of a function and whos(f::Function) to get a listing of the names and types of all the variables in the function.

whos's output is modeled on the output of the existing methods in Base:

julia> function xyz(x::Int,y)
         p = pi
         z = x + y * pi
xyz (generic function with 1 method)

julia> whos(xyz)
	#s38	Any
	p	MathConst{:π}
	x	Int64
	y	Any
	z	Any


This method goes through the descendents of a given type and finds what methods are implemented for them. It returns a list of (Symbol,Float64) tuples, where the Symbol is the name of a function and the Float64 is the percentage of subtypes whose methodswith shows a result for that function.

Here's an example of calling it:

julia> using TypeCheck

julia> methodswithdescendants(Real)
10-element Array{(Symbol,Float64),1}:

julia> methodswithdescendants(Real;onlyleaves=true)
10-element Array{(Symbol,Float64),1}:

julia> methodswithdescendants(Real;onlyleaves=true,lim=20)
20-element Array{(Symbol,Float64),1}:

Other Ways to Run Checks

If you want to run these only on a single method, you can get the Expr for the method from code_typed and then pass that into the check you would like to run.