Stream: helpdesk (published)

Topic: Customize printing inside a stacktrace?


view this post on Zulip Mason Protter (Mar 01 2021 at 19:57):

Is there a way to customize how something is printed ina stacktrace? My usecase is the LegibleLambas.jl package which is made for making anonymous functions easier to read:

julia> f = (x -> x + 1)
(x -> x + 1)

but unfortunately, this is only half useful since I don't know how to make it print nicely in a stacktrace. E.g.:

julia> g(x) = f(x)
g (generic function with 1 method)

julia> g("hi")
ERROR: MethodError: no method matching +(::String, ::Int64)
Stacktrace:
 [1] (::var"#19#20")(x::String)
   @ Main ./REPL[34]:1
 [2] (::LegibleLambda{var"#19#20"})(args::String)
   @ LegibleLambdas ~/.julia/dev/LegibleLambdas/src/LegibleLambdas.jl:44
 [3] g(x::String)
   @ Main ./REPL[35]:1
 [4] top-level scope
   @ REPL[36]:1

It'd be nice if [2] here could say (x -> x + 1)(args::String)instead

view this post on Zulip Simeon Schaub (Mar 01 2021 at 21:05):

You are looking for Base.show_signature_function, which is not exported, so use at your own risk. Only the type information is preserved in stacktraces though, so you don't get an instance of f, but only the type of f.


Last updated: Nov 06 2024 at 04:40 UTC