[e-lang] Hyper-printing
Mark S. Miller
markm at cs.jhu.edu
Sat Jan 27 01:27:56 CST 2007
Based on a suggestion by Kevin Reid
Currently, ignoring various syntactic conveniences, a typical __printOn method
might look something like:
to __printOn(out :TextWriter) {
out.print("<")
out.print(mySubPartA)
out.print(", ")
out.print(mySubPartB)
out.print(">")
}
The TextWriter renders the pieces into a string to be presented, perhaps to a
human. When it encounters the sub-part objects, if it checks if it's one of
the few cases it knows about specially. If not, then it recursively calls
__printOn on them, passing to them a TextWriter much like itself.
When the containing object intends to encapsulate these parts while still
allowing them to be rendered, then it must use the :TextWriter guard on the
out parameter. This ensures that the object passed in is an actual TextWriter.
Correct code may rely on a TextWriter not to make these parts available, and
to invoke them only for purposes of printing them.
Kevin points out that there are use cases where 1) the containing object does
not encapsulate these objects anyway (which we knew), and 2) where it would be
useful for a TextWriter-like rendering object bound to out to create a
hypertext-like rendering, where the text created as the rendering of, for
example, mySubPartA, were associated with access to that object itself. For
example, that text could act like a link that, when clicked on, brought up a
window for interacting with that associated object.
If we want to support both possibilities, we need to
1) make the safe way the easy way. The current way is both safe and easy, so
we're cool.
2) make it possible for an object's __printOn to effectively give permission
to the TextWriter-like out argument to make its provided parts available from
the rendering it is creating.
Given E's guards and coercion system, I think we can make this work by
introducing a :HypertextWriter guard. An out that passes :HypertextWriter may
provide such access, and it may be further coerced by :TextWriter to create a
TextWriter that may be relied upon not to provide such access. If our
hypothetical container wishes to provide access to mySubPartA but not
mySubPartB, it could say:
to __printOn(out :HypertextWriter) {
out.print("<")
out.print(mySubPartA)
out.print(", ")
(out :TextWriter).print(mySubPartB)
out.print(">")
}
Of course, a fully encapsulating container and a fully non-encapsulating
container would be even simpler.
I believe all this can be done without any changes to the existing E system.
--
Text by me above is hereby placed in the public domain
Cheers,
--MarkM
More information about the e-lang
mailing list