Description
Currently, repr
in Julia is maybe evaluatable or parseable or (often) just a string representation only consumable by humans. However, it could be much useful if it is guaranteed to be evaluatable. For example, you can find a lot of examples in base/loading.jl
which is used as simple IPC mechanism:
Lines 1148 to 1151 in 9a8b2fd
In case of base/loading.jl
it is OK to use repr
because the types used there are known to have evaluatable repr
. However, some basic types like PkgId
cannot be used in this way:
julia> repr(Base.PkgId(InteractiveUtils))
"InteractiveUtils [b77e0a4c-d291-57a0-90e8-8db25a27a240]"
Another concrete example I have in mind where repr
can be useful is to create a plot "spec" using DataVoyager.jl and dump it into a runnable Julia script. Currently, using it needs a rather convoluted invocation like show(IOContext(stdout, :compact=>false), "text/plain", spec)
because repr
is not a reliable entry point for evaluatable code.
We have Serialization
module for serializing complex Julia objects. Unfortunately, it cannot be used for communicating with different Julia versions. For complex serializations it is probably a good idea to use something like JSON instead of Julia code. However, I think the above examples are good enough motivation to have repr
for simple cases.
It was suggested to add a flag like parseable
(e.g., #33178 (comment), #30683 (comment)) via the IOContext
mechanism. I have a concern about this direction because it is practically impossible to enforce parsability this way (a brief discussion with @JeffBezanson in #30757 (comment)). I think the overloading API should be clear that the implementer has to make it evaluatable. This can be done by introducing overloading API such as show(::IO, ::MIME"application/julia", obj)
or repr(::IO, obj)
.
Questions:
- Do we need an API to produce Julia code that is (very likely to be) evaluatable? (100% guarantee is impossible when using different versions of libraries and/or Julia)
- How should it be implemented? Based on
IOContext
? New entry point (e.g.,show(::IO, ::MIME"application/julia", obj)
)? Something else?