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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With