I've implemented a prototype
show() function
recshow() that handles self-referential objects.
Examples:
require("recshow.jl") type T; x; y; end type Unshowable; end show(io::IO, x::Unshowable) = error("Tried to show Unshowable()") println("Simple and tree structured objects: just as show") recshow(1.55); println() recshow(155); println() recshow("abc"); println() recshow(T(1,2)); println() println("\nSelf-referential object:") s = T(1,1); s.y = s recshow(s); println() println("DAG structured object:") t = T(1,1) recshow(T(t,t)); println() println("Exception during show recording ==> partial printout:") recshow(T(s, Unshowable()))produces
Simple and tree structured objects: just as show 1.55 155 "abc" T(1, 2) Self-referential object: <obj> = T(1, <obj>) DAG structured object: <obj> = T(<x1>, <x1>) <x1> = T(1, 1) Exception during show recording ==> partial printout: <obj> = T(<x1>, #encountered exception! <x1> = T(1, <x1>) Exception in recshow: Tried to print Unshowable() in recshow at recshow.jl:158 in recshow at recshow.jl:163 in load at util.jl:230 in load at util.jl:242 at test.jl:28 in load at util.jl:253I think it would be nice to eventually replace the use of
show() in the REPL by this kind of functionality.
It should be helpful in working with any kind of data structure that is not a tree;
e g in my own DAG coding I've been swamped by repeat printouts.
To create a place to detect cycles and repeats, I've had to split the use of
show() into two parts:
- When creating a new type, implement a show() method to show it
- When showing an object, use rshow()
To make the cycle detection work with a given type, calls to
show inside of
show itself must be replaced by rshow. (better name, anyone?)
This is where I need help, and feedback!
Do people think this is a good way to go about it?
/ Toivo
ps.
I've attached my current recshow.jl, which should hopefully reproduce the examples above.
The tricks to capture strings going to the IO seem to be a bit different between the julia versions,
so please let me know if it fails.
The code captures the entire output of
show into a graph structure, manipulates it a bit, and then prints it.
It also contains a default
show implementation for composite types written in julia, that uses
rshow on the field's values.
I made
recshow() inline short printouts of repeated objects, but only if they are immutable.
This should prevent losing info about object identity in the printout.
/ Toivo