More compact type info in stacktrace/errors for Unitful.jl

Is it possible to somehow reduce the verbosity of the stacktrace regarding the type info. I rely on the use of units with unitful.jl and on the color types used within Images.jl. Btw. both packages are fantastic. Unfortunatelly, with their types, reading the error information is a nightmare.
For example, just the warning that I redefine the function:

function GetLensalignWEGSMF(Ek0::Unitful.Energy, zfocus::Unitful.Length, zXover::Unitful.Length; 
        dX0=[.0, .0, .0]u"mm", dXEBA=[.0, .0, .0]u"mm")
    return zfocus+zXover
end

will produce a unreadable block of lines like this:

WARNING: Method definition GetLensalignWEGSMF(Unitful.Quantity{T<:Any, 
Unitful.Dimensions{(Unitful.Dimension{:Length}(power=Base.Rational{Int64}(num=2, den=1)), 
Unitful.Dimension{:Mass}(power=Base.Rational{Int64}(num=1, den=1)), Unitful.Dimension{:Time}
(power=Base.Rational{Int64}(num=-2, den=1)))}, U<:Any}, Unitful.Quantity{T<:Any, 
Unitful.Dimensions{(Unitful.Dimension{:Length}(power=Base.Rational{Int64}(num=1, den=1)),)}, 
U<:Any}, Unitful.Quantity{T<:Any, Unitful.Dimensions{(Unitful.Dimension{:Length}
(power=Base.Rational{Int64}(num=1, den=1)),)}, U<:Any}) in module Main at In[44]:2 overwritten at
 In[45]:3.
WARNING: Method definition #GetLensalignWEGSMF(Array{Any, 1}, Main.#GetLensalignWEGSMF, 
Unitful.Quantity{T<:Any, Unitful.Dimensions{(Unitful.Dimension{:Length}(power=Base.Rational{Int64}
(num=2, den=1)), Unitful.Dimension{:Mass}(power=Base.Rational{Int64}(num=1, den=1)), 
Unitful.Dimension{:Time}(power=Base.Rational{Int64}(num=-2, den=1)))}, U<:Any}, 
Unitful.Quantity{T<:Any, Unitful.Dimensions{(Unitful.Dimension{:Length}(power=Base.Rational{Int64}
(num=1, den=1)),)}, U<:Any}, Unitful.Quantity{T<:Any, 
Unitful.Dimensions{(Unitful.Dimension{:Length}(power=Base.Rational{Int64}(num=1, den=1)),)}, 
U<:Any}) in module Main overwritten.

As you see, currently the ease of use of julia error reports is almost reaching the level of the famous C++ compiler error reports (with templates).
Is there a way how to somehow simplify the output so instead of the full type definitions I could see the type aliases I used in the method definition?

Thank you
Petr

3 Likes

I agree, those error message are really hard to read. I got a PR accepted that helps in some limited cases, but a more general solution would be great.

With Unitful master:

julia> 1u"m"+2u"s"
ERROR: DimensionError: 1 m and 2 s are not dimensionally compatible.
 in +(::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Length} .                         
(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Meter,Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}} .   
(0,1//1),),Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}}},   
::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time} . 
(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}
(0,1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}) at /Users/user/.julia/v0.5/Unitful/src/Unitful.jl:467

Stack traces in v0.6 are also a bit more legible, which helps in these cases.

I had an old package where I overloaded show on the type to avoid the module name prefix, which also helps a bit.

@andyferris thank you for the answer. I was thinking of replacing the output string by set of regexes for my most used (and annoying) types. Not systematic or elegant solution but will make my life easier. Could you point me to the code where you overloaded the show method in the package so i can write my own version? In which repository is the code you mentioned?

Is this sufficient?

julia> struct Foo; end  # or immutable, on v0.5

julia> Base.show(io::IO, ::Type{Foo}) = print(io, "Fooooooo....")

julia> Foo
Fooooooo....

I’ve just tried out the stacktrace enhancement on Julia 0.6 and my feeling is that the stacktraces for complex types are much clearer.

julia> 1u"s" + 1u"mm"
ERROR: DimensionError: 1 s and 1 mm are not dimensionally compatible.
Stacktrace:
 [1] +(::Quantity{Int64, Dimensions:{?}, Units:{s}}, ::Quantity{Int64, Dimension
s:{?}, Units:{mm}}) at c:\Users\phlavenk\Documents\programs_test\juliadata\v0.6\
Unitful\src\Unitful.jl:467

Actually its much better on the REPL where important things are highlighted using colors. KristofferC did really good work.
And thank you all for your suggestions.

2 Likes