THIS PAGE IS OBSOLETE! |
---|
It documents a proposal that was ultimately not adopted by the TCT. Modern Tcl code should use the expansion syntax {*}… instead. |
First of all, the patch for this is available from:
http://sourceforge.net/tracker/index.php?func=detail&aid=684534&group_id=10894&atid=310894
lconvert command, expands lists inline...
This patch adds a new command called lconvert and a new Tcl_ObjType called "plist". A "plist" or "parameter list" is identical to a list with the LARGE exception that when a "plist" is used in ANY command, it is expanded to as many "real arguments" as it has list elements.
The syntax of the lconvert command is as follows:
where "varName" is the name of a pre-existing LIST or PLIST variable to convert.
where "type" is optional and if specified, must be "list" or "plist".
if "type" is omitted, the current type of the list variable is returned, otherwise, the newly converted list is returned.
an error is returned if you try to convert a non-list, a non-existant variable, or if you try to convert a list to the type it currently is.
WARNING: This patch MAY VERY WELL break a lot of things. It is meant only as a proof-of-concept. If and when all the quirks are worked out, a TIP will be forthcoming. You have been warned.
A typical case where things don't work as expected:
This does not work as some people would expect. And to those uninitiated in the Zen Mastery of Tcl, it's non-obvious how to fix it:
% set x [list 1 2 3 4] 1 2 3 4 % file join $x 1 2 3 4
Now, let's try with lconvert:
% set x [list 1 2 3 4] 1 2 3 4 % lconvert x plist 1 2 3 4 % file join $x 1/2/3/4
Normally, file join takes a variable number of arguments and joins them together with a path separator. In this case, we only passed one real argument (a "plist") and it was expanded automatically to four "real arguments". This can also be accomplished with eval, although that is not as safe.
This is the current "solution" to this situation (use eval):
% set x [list 1 2 3 4] 1 2 3 4 % eval file join $x 1/2/3/4
REVISION HISTORY
JJM 2003/02/11 -- initial version 1.
MS remarks that this approach does change Tcl's syntax rules, and in particular voids rule 11 in the Endekalogue. If the meaning is to also allow its usage as
file join [lconvert $x]
as I surmise, it does so by having [lconvert $x] return more than one word (in the sense of the syntax rules), which violates rule 6.
Let me also remark that if the method requires a new type of Tcl_Obj, then it most certainly violates some rule in the Endekalogue, i.e., introduces new syntax. Tcl_Obj's are an implementation detail, but do not appear in Tcl's syntax by design. Except for optimisation issues, the script writer can be completely oblivious to their existence. The short version: everything is a string. Tcl's words are like black holes, they have no hair ...
RS 2008-10-06: Isn't this made obsolete, by 8.5 and a new syntax rule, in that the now built-in
file join {*}$x
serves the same purpose?