Stream: helpdesk (published)

Topic: `show` methods


view this post on Zulip Expanding Man (Feb 22 2021 at 17:48):

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.

view this post on Zulip Chad Scherrer (Feb 22 2021 at 17:52):

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])

view this post on Zulip Chad Scherrer (Feb 22 2021 at 17:52):

Well that's just one sample, it's what happened to be in my REPL :smile:

view this post on Zulip Chad Scherrer (Feb 22 2021 at 17:54):

Very much a work in progress, buy here's the rough idea:
https://github.com/cscherrer/NestedTuples.jl/blob/dev/src/summarize.jl

view this post on Zulip Expanding Man (Feb 22 2021 at 17:55):

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.

view this post on Zulip Expanding Man (Feb 22 2021 at 17:55):

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

view this post on Zulip Chad Scherrer (Feb 22 2021 at 17:56):

Yeah it would be great to have a nice composable solution to this, but I don't know of anything

view this post on Zulip Expanding Man (Feb 22 2021 at 17:56):

In the long run the solution is probably to have much more useful behavior from IOContext(io, :compact=>true), possible with more options.

view this post on Zulip Chad Scherrer (Feb 22 2021 at 17:57):

Yep the default still has a silly number of decimal places, and an easy way to adjust it would be really great

view this post on Zulip Expanding Man (Feb 22 2021 at 17:59):

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.

view this post on Zulip Chad Scherrer (Feb 22 2021 at 18:08):

It does make output shorter, just not short enough in many cases

view this post on Zulip Rafael Fourquet (Feb 22 2021 at 18:10):

I have a hack in one of my packages, maybe this helps:

Base.show_type_name(io, typeof(s).name)

view this post on Zulip Gustavo Goretkin (Feb 23 2021 at 02:59):

even Base sort of has this problem: https://github.com/JuliaLang/julia/issues/36263


Last updated: Nov 06 2024 at 04:40 UTC