The Julia Debugging Cheat Sheet

Not a bad idea, but I don’t think the chart would look much different for Julia. Most of the standard debugging practices under “no” still apply. Some of the error names are different. The list of possible errors are described in the manual here.

Common Errors

The most common error in Julia is probably MethodError which means you did not pass values of the correct type or in the correct order to a function. The fix is to look at the docstring for the function with ?function_name to see what the function expects.

Another error you might see is UndefVarError which might mean you didn’t load the package with using PackageName yet.

Common Expectations

I am trying to think of Julia specific “gotchas”, but not coming up with many inside of Base Julia.

First

Probably the biggest is that loops do not know about global variables unless you use the global keyword, so the following will not work when run from a file unless you uncomment the third line.

j=1
while j<10
    # global j
    println(j)
    j+=1
end

However, Julia lets you know about this now with a warning.

┌ Warning: Assignment to `j` in soft scope is ambiguous because a global variable by the same name exists: `j` will be treated as a new local. Disambiguate by using `local j` to suppress this warning or `global j` to assign to the existing global variable.
ERROR: LoadError: UndefVarError: j not defined

Second

Parentheses are not allowed for indexing, but again Julia’s error message lets you know.

julia> v(2)
ERROR: MethodError: objects of type Vector{Int64} are not callable
Use square brackets [] for indexing an Array.

Third

Lastly, assigning an array to a variable does not copy it unless you ask.

julia> A=[1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> B=A
3-element Vector{Int64}:
 1
 2
 3

julia> C=copy(A)
3-element Vector{Int64}:
 1
 2
 3

julia> A[1]=50
50

julia> println(A,B,C)
[50, 2, 3][50, 2, 3][1, 2, 3]

Others

Some others are in this post, but they are somewhat more advanced.

6 Likes