About 90% of the time, if I bother writing a show
method, its just to get something far less verbose than the default method. Very often, I start this with show(io, typeof(arg))
. However, if my argument has a ton of type parameters, even this is verbose. I could just do show(io, "NameOfStruct")
but this loses some of the niceties of the built in method, in particular, it does not prepend the module name if the struct is unexported.
Any advice for how to easily get this behavior, or more general advice for defining more compact show methods? Much to my disappointment, IOContext(io, :compact=>true)
almost always seems useless.
I have a similar problem for MCMC samples. I'm approaching it by having a summarize
method and defining and propagating methods as I go. For example for named-tuple samples it's something like
(σ = 0.76±0.0, α = 2.3±0.0, β = [0.51±0.0, -0.94±0.0, -0.43±0.0, 0.25±0.0, -0.39±0.0])
Well that's just one sample, it's what happened to be in my REPL :smile:
Very much a work in progress, buy here's the rough idea:
https://github.com/cscherrer/NestedTuples.jl/blob/dev/src/summarize.jl
Yeah, most of the time what I do is something like
function show(io::IO, s::SomeStruct)
show(io, typeof(s))
show(io, (a=s.a, b=s.b))
end
which works kind of ok, but the named tuple is even more verbose than I'd like, for example I really don't want the spaces around the =
if it makes it too verbose.
but like I said, the above gets ruined if SomeStruct
has a ton of elaborate type parameters, in that case I have to do show(io, "SomeStruct")
but run into the problems I mentioned above
Yeah it would be great to have a nice composable solution to this, but I don't know of anything
In the long run the solution is probably to have much more useful behavior from IOContext(io, :compact=>true)
, possible with more options.
Yep the default still has a silly number of decimal places, and an easy way to adjust it would be really great
Unfortunately it can't be easily extended without type piracy, so to do this in a demo package would take re-implementing IOContext
. Might be a good little project to copy in paste it with the intention of eventually adding the re-worked version to Base
. The fact that :compact=>true
seems useless to me might suggest that it's used in some context I'm just completely missing.
It does make output shorter, just not short enough in many cases
I have a hack in one of my packages, maybe this helps:
Base.show_type_name(io, typeof(s).name)
even Base sort of has this problem: https://github.com/JuliaLang/julia/issues/36263
Last updated: Nov 22 2024 at 04:41 UTC