\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#58: Extend [set] to Assign Multiple Values to Multiple Variables}
\date{September 02, 2001}
\author{Anselm Lingnau}
\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 \#58}&\textbf{Extend [set] to Assign Multiple Values to Multiple Variables}\\\hline
Author&

Anselm Lingnau \tipmail{anselm}{strathspey.org}
\\
Created&Sunday, \(\text{2}^{\text{nd}}\) September 2001\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.6 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a multiple assignment command as a backwards-compatible extension to the Tcl \textit{set} command.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Introduction}

Often one needs to assign values to several variables in close proximity. Right now several \textit{set} commands are necessary:
\begingroup\small\begin{verbatim}
  set a 123
  set b 456
\end{verbatim}
\endgroup

or
\begingroup\small\begin{verbatim}
  set a 123; set b 456
\end{verbatim}
\endgroup

Or one abuses the \textit{foreach} command:
\begingroup\small\begin{verbatim}
  foreach {a b} {123 456} break
\end{verbatim}
\endgroup

However, by analogy to the \textit{variable} and \textit{array set} commands, the following would be useful:
\begingroup\small\begin{verbatim}
  set a 123 b 456
\end{verbatim}
\endgroup

This would assign 123 to the variable \textit{a} and 456 to the variable \textit{b}.

Note that this extension is backwards-compatible to existing uses of the \textit{set} command since until now only one or two arguments to \textit{set} were allowed.

\section{Specification}

The \textit{set} command is extended to allow either one or an even number of arguments. The behaviour in the case of one argument remains the one documented in the \textit{set} manual page; when an even number of arguments is specified, the behaviour of \textit{set v0 e0 ... vn en} is identical to that of the sequence of commands \textit{set v0 e0; ...; set vn en} according to the traditional semantics, except that the way Tcl processes commands means that \textit{e0} ... \textit{en} are all evaluated before any assignments are performed. I.e., the commands
\begingroup\small\begin{verbatim}
  set a 1
  set a 2 b $a
  puts $b
\end{verbatim}
\endgroup

print \textit{1}, not \textit{2}. If this is an issue you must use separate \textit{set} statements.

The command \textit{set v0 e0 ... vn en} returns the value of \textit{en}.

\section{Rationale}

This extension is an obvious analogy to the \textit{variable} and \textit{array set} commands of Tcl, both of which allow an alternating list of names and expressions to be given as arguments. It is completely backwards-compatible (\textit{set} invocations with more than two arguments used to be syntax errors) and very easily implemented.

This extension in no way prejudices against the adoption and use of other multiple-assignment commands, such as \textit{lassign} (see \cite{tip57}). In particular, the \textit{set} extension is unsuitable for assigning a list result to a number of variables element by element. However, its simplicity and consistency to other similar Tcl commands is appealing.

\section{Reference Implementation}

A patch to Tcl 8.4a3 which implements the \textit{set} extension may be found at \url{http://anselm.our-isp.org/set-patch.diff} --- a patched Tcl 8.4a3 passes the Tcl 8.4a3 regression test suite with no test failures. No test cases nor documentation for the \textit{set} extensions have been devised yet but this is easy to do once there is a consensus that this feature is actually desirable.

\section{Copyright}

This document is placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#57]{tip57}
Donal K. Fellows,
Agnar Renolen,
Don Porter,
\emph{Move TclX's [lassign] into the Tcl Core},
on-line at \url{http://purl.org/tcl/tip/57.html}
\end{thebibliography}
\end{document}
