<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Mon May 20 18:22:00 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='57'>
<header><title>Move TclX&apos;s [lassign] into the Tcl Core</title><author address="mailto:fellowsd@cs.man.ac.uk">Donal K. Fellows</author><author address="mailto:agnar.renolen@emap.no">Agnar Renolen</author><author address="mailto:dgp@users.sf.net">Don Porter</author><status type='project' state='final' tclversion="8.5" vote='after'>$Revision: 2.4 $</status><history></history><created day='30' month='aug' year='2001' /></header>
<abstract>This TIP proposes to move the <emph style="italic">lassign</emph> command from the TclX extension into the Tcl core to make multiple assignment a much easier process for people.</abstract>
<body><section title="Rationale">
<para>In many cases, a command needs to return more than one return value to the caller. For example, suppose that the statement:</para>
<verbatim><vline encoding='base64'>IHNldCBjb29yZHMgW0xvY2F0ZUZlYXR1cmUgJGZlYXR1cmVJRF0=</vline></verbatim>
<para>would set the variable &quot;coords&quot; to a list containing two elements &quot;x&quot; and &quot;y&quot;. Assume that you need to set the &quot;x&quot; and &quot;y&quot; components directly, you can do this today using the following statement:</para>
<verbatim><vline encoding='base64'>IGZvcmVhY2gge3ggeX0gW0xvY2F0ZUZlYXR1cmUgJGZlYXR1cmVJRF0ge30=</vline></verbatim>
<para>Now, this is not what the <emph style="italic">foreach</emph> command was designed for, and it is not obvious at first glance from the source code what the statement does. Although it is quite useful for the purpose described in this TIP, It would be more logical if the developer could write the following:</para>
<verbatim><vline encoding='base64'>IHNldCB7eCB5fSBbTG9jYXRlRmVhdHVyZSAkZmVhdHVyZUlEXQ==</vline></verbatim>
<para>or</para>
<verbatim><vline encoding='base64'>IG1zZXQge3ggeX0gW0xvY2F0ZUZlYXR1cmUgJGZlYXR1cmVJRF0=</vline></verbatim>
<para>However, there is already a command in TclX for doing this kind of operation: [lassign]. Given that many people already know TclX, importing the command from there makes a great deal of sense. It also has the nice feature of returning those list items that were not assigned, making it easy to strip a few words off the front of a list. That sort of operation is useful when performing tasks like command-line option parsing.</para>
</section>
<section title="Proposal">
<para>Define a new command in Tcl called [lassign] with the following syntax (<emph style="italic">$val</emph> indicates an argument that the caller would supply):</para>
<verbatim><vline encoding='base64'>IGxhc3NpZ24gJGxpc3RWYWx1ZSAkdmFyTmFtZSA/JHZhck5hbWUgLi4uPw==</vline></verbatim>
<para>The command interprets its first argument as a list value and all subsequent arguments as variable names. The first item in the list value (i.e. at index 0) will be assigned to the first variable named, the second item in the list value will be assigned to the second variable named (if present), etc. When there are more variables than list items, the remaining variables will be assigned the empty string. The result of the command is a sublist of the input list-value that contains only items that were not assigned to a variable; if all values were assigned, the result is an empty list.</para>
<para>This is exactly the specification of the behaviour of the correspondingly-named command in TclX.</para>
</section>
<section title="Notes">
<para>It should be possible to efficiently compile [lassign] in many cases, which would make a tremendous difference in execution speed over not only the TclX version of [lassign], but also over the [foreach] &quot;idiom&quot;, especially when assigning to variables that are not simple local variables (the case which [foreach] compilation is optimized for.) For this reason, I&apos;m not committing to implementing [lassign] using the TclX code.</para>
<para>This TIP was substantially different in the past. Please view the CVS history for details.</para>
</section>
<section title="Copyright">
<para>This document is placed in the public domain.</para>
</section>
</body></TIP>
