Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a type alias instead of a parametric type in error messages

Tags:

julia

I build a parametric type in julia:

type MyType{T}
    x::T
end

and for simplicity, I build a type alias for Float64:

typealias MT MyType{Float64}

I now deliberately cause an error involving MT. For example:

y1 = MyType(1.0)
y2 = MyType(2.0)
y1 + y2

will throw an error because + is not defined for MyType. The error message says:

`+` has no method matching +(::MyType{Float64}, ::MyType{Float64})

I would like it to say:

`+` has no method matching +(::MT, ::MT)

Why? Because real-world examples sometimes get quite a bit more complicated than this toy example, and one purpose of a type alias is to make a complicated specific instance of a parametric type easily recognisable. So it would be nice to also make it easily recognisable in error messages.

What have I tried? My best guess is that the error function calls the string function over a DataType in order to generate the appropriate strings in the error message. So it isn't obvious to me that I can extend the string function via multiple dispatch to specialise on my type alias, so I'm pretty much at a loss about where to go from here.

like image 410
Colin T Bowers Avatar asked Jun 02 '15 05:06

Colin T Bowers


1 Answers

You need to define an appropriate show method:

import Base.show
show(io::IO, ::Type{MT}) = print(io, "MT")

Your example then gives:

julia> y1 + y2
ERROR: `+` has no method matching +(::MT, ::MT)
like image 74
Simon Byrne Avatar answered Sep 19 '22 13:09

Simon Byrne