-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use newlines and indents to make large, complex structs more readable when printed #43260
base: master
Are you sure you want to change the base?
Use newlines and indents to make large, complex structs more readable when printed #43260
Conversation
This would be great. What your example doesn't show is nested julia> struct Mine{A,B} a::A; b::B; end
julia> x = reshape(0:0.2:1, 2,3)'; Mine((1,2,x), Mine(3,(4,x))) # before
Mine{Tuple{Int64, Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}, Mine{Int64, Tuple{Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}}}((1, 2, [0.0 0.2; 0.4 0.6; 0.8 1.0]), Mine{Int64, Tuple{Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}}(3, (4, [0.0 0.2; 0.4 0.6; 0.8 1.0])))
julia> x = reshape(0:0.2:1, 2,3)'; Mine((1,2,x), Mine(3,(4,x))) # after
Mine{Tuple{Int64, Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}, Mine{Int64, Tuple{Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}}}(
(1, 2, [0.0 0.2; 0.4 0.6; 0.8 1.0]),
Mine{Int64, Tuple{Int64, Adjoint{Float64, Base.ReshapedArray{Float64, 2, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, Tuple{}}}}}(3,
(4, [0.0 0.2; 0.4 0.6; 0.8 1.0]))) (Note also that in this example, |
Seems consistent to adopt the dimmed color from stacktraces |
Seems like this should only be used for the 3-argument |
Can you say a bit more, @stevengj ? If I understand right, this should only be called if no specialized method is defined or provided. Or is there an example I can test to make sure it doesn't occur in the situation you're envisioning? |
87d1ece
to
ee1f27b
Compare
New state for your example @mcabbott : |
As explained here you should generally define a 3-argument That is, the old 2-argument |
Sure, but I'm not defining any What I'm asking is, in what situation would this not work, such that it has to be done differently? |
I think that the default at present only has one path, both for display and inline printing. If I understood right, the suggestion is that an upgrade like this should make it take two paths, so that the multi-line unfolded printing is only triggered when the struct is shown at top level, like for Array. How much code these two paths can share I don't know.
Cool. How hard would it be to delay the grey by one level, so that some type parameters are shown? |
Hmm... so The So... possible someone who knows the printing system better needs to help untangle this?
Moderate. One issue here is that the printing of types in stack traces is entirely separate logic. It's easier there to break down the parts; here, it's just calling |
Ah, just added detection of And unfortunately the gray only applies to the default display - it wouldn't impact any custom type displays with type parameters. |
Oh, I see... yes, unfortunately it seems quite hard to use this only for 3-argument |
Just revisiting this - So it seems like it shouldn't be the default, but perhaps as an optional path it could work? So that on-demand a struct could be printed in a slightly nicer way. What would the best way to do that be? |
I think https://github.com/thautwarm/PrettyPrint.jl attempts to address this? I am unsure how it deals with the dispatch question. In theory we could have something like But I think in this case, it may selectable with a boolean flag to the function, so let me try to write inline comments to suggest how to do that |
Ah, sorry, I mixed myself up. I think what is needed is to change the existing mime-show fallback method with this:
and add an optional boolean parameter to showdefault which sets whether to print "pretty", recursively, or not |
I checked out PrettyPrint.jl, and it's doing a bit too much IMO (see image). It's choosing to fill the screen vertically to show whole arrays (no truncation), and it failed on an UndefRefError with my example in the OP. I was just looking to add minimal extra structure to the standard printing, which I was otherwise injecting manually. Just enough to make it easier to read and compare complex nested types. |
Yes, take a look at the |
As shown in #43183 , printing of structs can get quickly out of hand. While there are nice solutions to better display structs, as noted there, they may be a bit too heavy for Base, or too similar to
dump
.This PR uses newlines and indents to add just a bit of visual structure to complex structs, so they can remain compact but more human-readable.
The rule in use is that if a struct contains other structs, or if the type as printed takes a large fraction of the
displaysize()
columns, it gets its own line. Simple structs which when printed do not take up many columns are printed inline as before.