2-arg show is currently not copy-pasteable.
My understanding is that 2-arg show(::IO, x) is meant to try to be copy-pasteable as much as possible. It's to this end that people came up with the clever var"" macro, and other such tricks. I think for the most part, Base tries to adhere pretty closely to this.
I was wondering whether we could improve the current 2-arg show, which prints like this:
julia> show(js)
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
to instead be a copy-pasteable output, valid at the REPL.
My current best idea is to just wrap the whole thing in JSON3.read("""...""")! I think this is pretty clever, and makes it simultaneously copy-pasteable, while still verbose and useful. What do you think?
For the above example, that would be:
julia> show(js)
JSON3.read("""{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}""")
Is that crazy?
Also, i think we would only need to do this for anything that recursively contained Dicts, since array-only structures already print as valid julia arrays?
I don't think of show as having the sort of contract as repr in Python where that really ought to be able to be evaluated and return the same thing as was printed. Is this convention documented? (It's very possible I just haven't come across this 😄)
Is that true of a lot of other data-oriented packages? DataFrames comes to mind as that output is much more for human consumption than copy/pasteability. And in a sense, this JSON3 output is copy/pasteable, just for use as JSON and not Julia code.
julia> show(d)
2×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 0
2 │ 2 0
If we do stick with the current printing, it would be nice to use a lower indent level, this gets very wide very fast.
Yeah, interesting. I have usually thought of show has having the same contract as repr, because the julia implementation of repr(x) calls show(::IO, x)!
My understanding is that it is a convention, and I think they (mostly) try to stick to it in Base, but I also do agree that it's not super widely followed, especially outside Base. And i'm not at all familiar with data-oriented packages, so i might be way off base :)
And in a sense, this JSON3 output is copy/pasteable, just for use as JSON and not Julia code.
This is the key/motivation behind current show method in JSON3.jl. At least for me, it's been more common/useful to be able to copy/paste JSON as opposed to copy/paste valid Julia JSON.
That said, I'd be fine defining a separate repr method that produced the requested Julia copy/paste format.