-
-
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
introduce IOContext and ImmutableDict to fix some of show, print, & friends #13825
Conversation
for a usage example take a look at the implementation of tvar_env 11a5c37#diff-b4267643a238b5a375b3e055817ee6caR833 or typeemphasize. once the IOContext implementation is in place, the addition of each of these is accomplished in a small amount of code. |
Did I miss you explaining the high-level idea of IOContext somewhere? |
there's not really much to explain, so I wasn't sure what to write. the problem in all of the IO issues above is that the show methods want to customize the output based on some property known only to the caller, but there was no way to pass that information into the callee -- other than TLS variables. IOContext is unlike any other IO object (well, except maybe Process), in that it is does not actually reflect a type of IO stream, but instead can be used to convey context about how the IO stream should be used (or ignored and used as a generic stream). |
Start with "what is an IO context?". Move on to "why do we need them?" Probably want to include "what problems do they solve?" |
an immutable Dictionary that is a subclass of IO. or alternatively, keyword arguments for all IO functions
the existing problems in the show/print hierarchy (ref #5709) are generally of the form that the caller only can provide two arguments: what to print and where to print. but the caller often wants to include a third consideration: how to print (limited, recursive, types-emphasized, typevars ignored, mime formatting).
this PR provides a mechanism to essentially add keyword arguments to every function ever written for IO, without changing a single line of existing code (by transparently reusing the existing IO argument) |
|
||
limit_output(::ANY) = false | ||
limit_output(io::IOContext) = io.limit_output | ||
function in{I<:IOContext}(value, io_key::Pair{I}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this notation really confusing. Normally we'd have (key=>val) in dict
, and this rearranges it to val in (dict=>key)
. There can be no good reason for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point. this was holdover from an earlier, more convoluted design iteration. this can be switched now in a future rev of the PR.
4ccd153
to
265670c
Compare
bump. i completed the implementation of this proposal over the weekend and would appreciate review. |
d106acc
to
96b83ad
Compare
I haven't reviewed the first few commits, but I've looked at some the rest, and it definitely looks like a good idea. Elimination of those |
96b83ad
to
7eebc4e
Compare
7eebc4e
to
9875f49
Compare
this is designed to be used as the basis for the IOContext type also fix a bug in testing equality of a self-referential dict (with added test)
…d in a new IOContext lightweight wrapper type
…ting of a method signature for example, this turns: rand{T<:Union{Bool,Int16,Int32,Int64}}(::Type{T<:Union{Bool,Int16,Int32,Int64}}) at random.jl:38 into: rand{T<:Union{Bool,Int16,Int32,Int64}}(::Type{T}) at random.jl:38 and it resolves ambiguous expressions such as: foo{T,N}(::Array{T,N}, ::Vector, ::Array) which used to be printed as: foo{T,N}(::Array{T,N}, ::Array{T,1}, ::Array{T,N}) by printing it instead as: foo{T,N}(::Array{T,N}, ::Array{T<:Any,1}, ::Array{T<:Any,N<:Any})
9875f49
to
52fecaf
Compare
this now has tests and the actions items i had are closed. (this does not yet implement any of the changes discussed in #14052 as those were largely independent) |
counterpoint: it has near zero value without this PR (e.g. i wouldn't want to add it to base unless this PR also simultaneously got merged -- hence one PR) |
Then how much value does it have within this PR? Is it really necessary? |
it is an implementation detail for IOContext |
introduce IOContext and ImmutableDict to fix some of show, print, & friends
This is very nice, thanks for doing this. Looks like these |
See also #14533. |
I would rename We also need to figure out a better way to deal with properties like |
maybe if we don't want more exports, it's hard to get more concise than just using the definition everywhere: i'm not opposed to that. the named function version was a bit of an experiment in transitioning, but i didn't find it to be any more usable |
+1 for not exporting display-property-checker functions |
|
for (key, value) in l | ||
if value != get(r, key, secret_table_token) | ||
for pair in l | ||
if !in(pair, r, ==) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the type constraint is due to the expectation that an Associative only contains Pairs (e.g. see the error message you get if you look for something else: https://github.com/JuliaLang/julia/pull/13825/files#diff-d4736682004c1996e5d9b1c5c33c8c8eR28)
Issue caused by changes to alignment and related method signatures in the following PR: JuliaLang/julia#13825
Issue caused by changes to alignment and related method signatures in the following PR: JuliaLang/julia#13825
Issue caused by changes to alignment and related method signatures in the following PR: JuliaLang/julia#13825
Issue caused by changes to alignment and related method signatures in the following PR: JuliaLang/julia#13825
Issue caused by changes to alignment and related method signatures in the following PR: JuliaLang/julia#13825
@vtjnash The |
@vtjnash Bump? Could you explain the purpose of |
i generally considered it internal in that you shouldn't need to call it unless you are implementing a new display. it returns a generic size suitable for use by IO streams that couldn't compute the actual size |
Either it should be documented, or it should be another (unexported) function. The risk here is that people porting from the deprecated |
Should ImmutableDict be exported/documented? It feels like it could be useful for defining a FrozenSet/FrozenDict in DataStructures. |
This is a small PR is intended to provide a general framework to address quite a few of the broader issues with show, print, & friends.
The specific implementation details is that this is an immutable dictionary that is a subclass of IO (so it can be used anywhere an IO object can be used), where the dictionary allows a display function to pass along arbitrary metadata to arbitrary other parts of the printing infrastructure.
As proof-of-concept examples, the PR replaces the existing global or TLS shown_set, limit_output, type-emphasis, with a simple IO-local mechanism, plus it provides a way to add a lock to any IO object.
Just a quick search nets the following list of existing issues for reference:
solves #5709 "Show, showcompact, showall, showlimited, print, repr, summary, and friends"
addresses #7959 "proposed cleanup of output functions"
mostly implements #10794 "Show of parameterized methods" (replacing PR #10861)
provides a mechanism for addressing #6117 "array display is a mess"
alternative/complement to PR #8987 "WIP: cleanup of output functions"
addresses #10009 Overload
print
for Markdown Stringsfixes #11364 integrate code_warntype with the output system
fixes #10327 print_color and println_color
additionally, cross-ref with:
PR #12929 showcompact(::RefValue) prints the contained object compactly
#6921 Pager support in the repl
PR #13256 Wrapper to annotate the MIME type of some data
Inspired by https://groups.google.com/forum/#!topic/julia-dev/uKPxXCQCU_A
TODO:
IOContext(::IO, ::IOContext)
integrate MIME printing (e.g. WIP: cleanup of output functions #8987)make:formatting => :ansi
another valid option