\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#103: Argument Expansion Command}
\date{June 15, 2002}
\author{Peter Spjuth, Donal K. Fellows, Andreas Leitgeb}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#103}&\textbf{Argument Expansion Command}\\\hline
Author&

Peter Spjuth \tipmail{peter.spjuth}{space.se}
\par
Donal K. Fellows \tipmail{donal.k.fellows}{man.ac.uk}
\par
Andreas Leitgeb \tipmail{avl}{logic.at}
\\
Created&Saturday, \(\text{15}^{\text{th}}\) June 2002\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.13 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to add a command that can perform argument expansion in a safe and efficient manner.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Introduction}

Many commands take a variable number of arguments and often you find yourself with those arguments in a list. This list must then be expanded into individual arguments to the command. This is currently done with eval:
\begingroup\small\begin{verbatim}
eval destroy [winfo children .]
\end{verbatim}
\endgroup

This is a bit obscure and also very error prone when the command becomes more complex. It is also inefficient and not object safe, why a command specialised in doing this would be better.

\section{Rationale}

There have been suggestions of introducing some new syntax to Tcl to handle argument expansion. That is a big and controversial step, and not anything this TIP wants to meddle in. A command can improve every point where eval has shortcomings and thus give a good result with less means. It can also serve as a bridge to a future global syntax.

Such a command can be done in several ways and below the choice in this TIP's specification is defended.

As examples three statements are used which will be repeated for different alternatives. This is the eval version:
\begingroup\small\begin{verbatim}
eval destroy [winfo children .]
eval button .b $stdargs -text \$mytext -bd $border
eval exec \$prog $opts1 [getMoreopts] \$file1 \$file2
\end{verbatim}
\endgroup

The eval version would be even more complex if the lists that are to be expanded are not known to be pure. To be really safe the last would be:
\begingroup\small\begin{verbatim}
eval exec \$prog [lrange $opts1 0 end] [lrange [getMoreopts] 0 end] \$file1 \$file2
\end{verbatim}
\endgroup

With the proposed command they become:
\begingroup\small\begin{verbatim}
expand { destroy `[winfo children .] }
expand { button .b `$stdargs -text $mytext -bd $border }
expand { exec $prog `$opts1 `[getMoreopts] $file1 $file2 }
\end{verbatim}
\endgroup

An alternative to having a local syntax is to point at the arguments that should be expanded, either by index:
\begingroup\small\begin{verbatim}
expand {end} destroy [winfo children .]
expand {2} button .b $stdargs -text $mytext -bd $border
expand {2 3} exec $prog $opts1 [getMoreopts] $file1 $file2
\end{verbatim}
\endgroup

Or by some flag mechanism:
\begingroup\small\begin{verbatim}
expand destroy + [winfo children .]
expand button .b + $stdargs -text - $mytext -bd $border
expand exec - $prog + $opts1 + [getMoreopts] - $file1 - $file2
\end{verbatim}
\endgroup

Those lack in writability/readability/maintainability in a disturbing manner.

For the choice of local syntax one goal is that it should not violate Tcl's rules, which simplifies implementation since Tcl's parser can do the job.

Any char that fulfils that could be used but the choice fell on ` for forward compatibility reasons. See below.

An alternative syntax could be using enclosing `` or some other enclosing construct like:
\begingroup\small\begin{verbatim}
expand { destroy <[winfo children .]> }
expand { button .b <$stdargs> -text $mytext -bd $border }
expand { exec $prog <$opts1> <[getMoreopts]> $file1 $file2 }
\end{verbatim}
\endgroup

Paired characters are good for delimiting things. Here is the beginning; here is the end. But this is not about a new way to delimit things. It is about indicating a boolean choice: expand or do not expand a word into multiple words. Whatever character is chosen to be that indicator, it should be a single, leading one. No pairs.

In the specification a restrictive rule was chosen that makes it an error to use ` in a way that do not fit. This is to make it easier to change things in the future should ideas come up for new features. E.g., should this become a global syntax in Tcl 9.0 it can be chosen a bit differently and be backward compatible with the expand command.

\section{Specification}

A new command ``expand'' is added. It takes one argument, which contains a Tcl script consisting of one command. The script may contain comments but only one command is permitted.

The command is processed in the following manner:
\begin{enumerate}
\item{}
Parse into words according to Tcl's standard rules.
\setcounter{enumi}{1}
\item{}
Any word starting with ` must be followed by a single variable or command substitution. The word is remembered and the ` is removed.
\setcounter{enumi}{2}
\item{}
Perform Tcl's normal execution steps on the new line up to the point where the command should have been called.
\setcounter{enumi}{3}
\item{}
Expand the arguments that should be expanded.
\setcounter{enumi}{4}
\item{}
Execute the command.
\end{enumerate}

The return value of expand is the return value of the command.

\textit{Note 1:} A word should really start with ` to trigger expansion which means that words like these are not expanded:
\begingroup\small\begin{verbatim}
cmd "`$temp" \`[something]
\end{verbatim}
\endgroup

\textit{Note 2:} Expansion is only performed with words like:
\begingroup\small\begin{verbatim}
cmd `$var `[somecmd $arg] `$arr([cmd $arg])
\end{verbatim}
\endgroup

Words like these are a syntax error:
\begingroup\small\begin{verbatim}
cmd `word` `$x,$y `[foo]xy[apa]
\end{verbatim}
\endgroup

\section{Forward compatibility}

One aspect of choosing a syntax here is to think about the future. Should there later be a wish for a global syntax for argument expansion it would be nice if it were the same as the one chosen in the expand command. If an agreement can be made for what may be acceptable in the future, this should affect the specification in this TIP.

If a single character like ` is chosen for a global expand syntax it means a backwards compatibility break. So, what chars are likely to be used by people and thus causing problems or confusion when backwards compatibility is broken?

Some food for thought about different chars:
\begingroup\small\begin{verbatim}
_     # Word char
:     # Gets ugly with namespace qualifiers:  :$::var
\end{verbatim}
\endgroup
\begingroup\small\begin{verbatim}
!   if !$var {...}
*   string match *$suffix $line
^   regexp ^$prefix $line
~   cd ~$user
|   open |$prog
.   button .$w ; glob -nocomplain .$str
=   wm geometry .e =$geo
@   .x conf -bitmap @$bmp -cursor @$cur
<   bind . <$left>  ; set html <$tag>
\end{verbatim}
\endgroup
\begingroup\small\begin{verbatim}
(   expr ($a + $b) * $c ;# Confuses paren-matching
)     # Odd enough as opening, but would confuse any paren-matching
\end{verbatim}
\endgroup
\begingroup\small\begin{verbatim}
+   expr $a +$b  ;# Same for any operator
-   
%
&   
?
/   open /$path   
\end{verbatim}
\endgroup
\begingroup\small\begin{verbatim}
'     # Makes more sense as enclosing?
`     # Makes more sense as enclosing?
>   exec foobar >/some/file
,   append recipients ,[join $header($ccL) ,]
\end{verbatim}
\endgroup
\begingroup\small\begin{verbatim}
{}  completely forward-compatible, as {} currently cannot be
    trailed by anything but whitespace. (This would limit the
    originally proposed global syntax change to the argument
    of the expand command)
\end{verbatim}
\endgroup

Example usage of those that seem reasonable:
\begingroup\small\begin{verbatim}
expand { exec $prog '$opts1 '[getMoreopts] $file1 $file2 }
expand { exec $prog `$opts1 `[getMoreopts] $file1 $file2 }
expand { exec $prog ,$opts1 ,[getMoreopts] $file1 $file2 }
\end{verbatim}
\endgroup

For comparison, the syntax that has been proposed earlier that would not break backwards compatibility:
\begingroup\small\begin{verbatim}
expand { exec $prog {}$opts1 {}[getMoreopts] $file1 $file2 }
expand { exec $prog {expand}$opts1 {expand}[getMoreopts] $file1 $file2 }
\end{verbatim}
\endgroup

\section{Discussion}

When first issued the TIP caused some discussion on c.l.t. Until a summary is made, here is the thread:

\url{http://groups.google.com/groups?th=9e77d5836b06ab1b}

Another thread about it:

\url{http://groups.google.com/groups?th=2ba287be87c2678c}

\section{Reference Implementation}

Patch \#570201

\url{http://sourceforge.net/tracker/index.php?func=detail&aid=570201&group_id=10894&atid=310894}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
