开发者

How can I teach Tcl to automatically print my object with a proc I provide?

I have an object that Tcl shows in the console as being its object id. How can I extend Tcl such that whenever objects of my type are printed, a special proc is automatically called that I provide to print their contents instead of just giving the object id?

Some more details: I am emulating a lisp list in Tcl which 开发者_如何学Gois built up out of cons cells that each have a car and a cdr. The list of 1 "two" 3 would be created with:

(cons 1 (cons "two" (cons 3 nil)))

which creates 3 cons cells. The top cons cell that has 1 in its car has a pointer to the second cons cell that has "two" in its car, etc.

With this representation, I wish for the above sample list to print out as:

(1 "two" 3)


I assume you're working at the C level. Basically, you register a function to do this in your Tcl_ObjType structure, in the updateStringProc field. Your function will need to produce a string rendering of your overall value (stored in a ckalloced string in the bytes field of the Tcl_Obj); how to do so is up to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜