Stream: helpdesk (published)

Topic: Cusomizing `MethodError`


view this post on Zulip Mason Protter (Jun 30 2021 at 22:47):

This has been bugging me in the back of my mind for ages now. Does anyone know of a way that issues like https://github.com/MasonProtter/LegibleLambdas.jl/issues/6 can be fixed?

Basically, I have a callable struct which stores a function and an expression, and is supposed to print that expression when you show the struct. I'd like errormessages to be able to use this info, but I'm not sure if there's a way or not. I see that https://docs.julialang.org/en/v1/base/base/#Base.Experimental.register_error_hint exists, but I'm not sure it's applicable here

view this post on Zulip Takafumi Arakaki (tkf) (Jul 01 2021 at 00:37):

Type treason? https://github.com/JuliaLang/julia/issues/22363#issuecomment-308472359

struct ShowExpr{Tag} end

const COUNTER = Ref(0)
const EXPRS = Dict{Int,Any}()

macro showexpr(ex)
    tag = COUNTER[] += 1
    EXPRS[tag] = ex
    :(ShowExpr{$tag}())
end

function Base.show(io::IO, ::Type{ShowExpr{Tag}}) where {Tag}
    print(io, "typeof(@showexpr ", EXPRS[Tag], ")")
end

function Base.show(io::IO, ::ShowExpr{Tag}) where {Tag}
    print(io, "@showexpr ", EXPRS[Tag])
end

Horrible code :laughing:

view this post on Zulip Clark C. Evans (Jul 02 2021 at 00:53):

If I'm understanding properly, I do think that Julia needs special handling of MethodError. Julia is based upon generic programming, and we expect users to hook into a given library's algorithms by implementing function methods for their types. When a user gets a traceback, one normally thinks something's wrong, when in fact, for MethodError, they've not done anything wrong so much as they are not done implementing. For those unfamiliar with Julia giving a MethodError traceback without really explaining what it means seems a bit hostile.


Last updated: Nov 06 2024 at 04:40 UTC