<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu Feb 09 07:34:35 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='58'>
<header><title>Extend [set] to Assign Multiple Values to Multiple Variables</title><author address="mailto:anselm@strathspey.org">Anselm Lingnau</author><status type='project' state='rejected' tclversion="8.5" vote='after'>$Revision: 1.6 $</status><history></history><created day='2' month='sep' year='2001' /></header>
<abstract>This TIP proposes a multiple assignment command as a backwards-compatible extension to the Tcl <emph style="italic">set</emph> command.</abstract>
<body><section title="Introduction">
<para>Often one needs to assign values to several variables in close proximity. Right now several <emph style="italic">set</emph> commands are necessary:</para>
<verbatim><vline encoding='base64'>ICBzZXQgYSAxMjM=</vline><vline encoding='base64'>ICBzZXQgYiA0NTY=</vline></verbatim>
<para>or</para>
<verbatim><vline encoding='base64'>ICBzZXQgYSAxMjM7IHNldCBiIDQ1Ng==</vline></verbatim>
<para>Or one abuses the <emph style="italic">foreach</emph> command:</para>
<verbatim><vline encoding='base64'>ICBmb3JlYWNoIHthIGJ9IHsxMjMgNDU2fSBicmVhaw==</vline></verbatim>
<para>However, by analogy to the <emph style="italic">variable</emph> and <emph style="italic">array set</emph> commands, the following would be useful:</para>
<verbatim><vline encoding='base64'>ICBzZXQgYSAxMjMgYiA0NTY=</vline></verbatim>
<para>This would assign 123 to the variable <emph style="italic">a</emph> and 456 to the variable <emph style="italic">b</emph>.</para>
<para>Note that this extension is backwards-compatible to existing uses of the <emph style="italic">set</emph> command since until now only one or two arguments to <emph style="italic">set</emph> were allowed.</para>
</section>
<section title="Specification">
<para>The <emph style="italic">set</emph> 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 <emph style="italic">set</emph> manual page; when an even number of arguments is specified, the behaviour of <emph style="italic">set v0 e0 ... vn en</emph> is identical to that of the sequence of commands <emph style="italic">set v0 e0; ...; set vn en</emph> according to the traditional semantics, except that the way Tcl processes commands means that <emph style="italic">e0</emph> ... <emph style="italic">en</emph> are all evaluated before any assignments are performed. I.e., the commands</para>
<verbatim><vline encoding='base64'>ICBzZXQgYSAx</vline><vline encoding='base64'>ICBzZXQgYSAyIGIgJGE=</vline><vline encoding='base64'>ICBwdXRzICRi</vline></verbatim>
<para>print <emph style="italic">1</emph>, not <emph style="italic">2</emph>. If this is an issue you must use separate <emph style="italic">set</emph> statements.</para>
<para>The command <emph style="italic">set v0 e0 ... vn en</emph> returns the value of <emph style="italic">en</emph>.</para>
</section>
<section title="Rationale">
<para>This extension is an obvious analogy to the <emph style="italic">variable</emph> and <emph style="italic">array set</emph> commands of Tcl, both of which allow an alternating list of names and expressions to be given as arguments. It is completely backwards-compatible (<emph style="italic">set</emph> invocations with more than two arguments used to be syntax errors) and very easily implemented.</para>
<para>This extension in no way prejudices against the adoption and use of other multiple-assignment commands, such as <emph style="italic">lassign</emph> (see <tipref type="text" tip="57"/>). In particular, the <emph style="italic">set</emph> 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.</para>
</section>
<section title="Reference Implementation">
<para>A patch to Tcl 8.4a3 which implements the <emph style="italic">set</emph> extension may be found at <url ref="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 <emph style="italic">set</emph> extensions have been devised yet but this is easy to do once there is a consensus that this feature is actually desirable.</para>
</section>
<section title="Copyright">
<para>This document is placed in the public domain.</para>
</section>
</body></TIP>

