Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TIP #58: Multiple Assignment Command (Take 2)

5 views
Skip to first unread message

Anselm Lingnau

unread,
Sep 3, 2001, 2:15:43 PM9/3/01
to

TIP #58: MULTIPLE ASSIGNMENT COMMAND (TAKE 2)
-----------------------------------------------
Version: $Revision: 1.1 $
Author: Anselm Lingnau <ans...@strathspey.org>
State: Draft
Type: Project
Tcl-Version: 8.4
Vote: Pending
Created: Sunday, 02 September 2001
URL: http://purl.org/tcl/tip/58.html
WebEdit: http://purl.org/tcl/tip/edit/58
Post-History:

-------------------------------------------------------------------------

ABSTRACT
----------

This TIP proposes a multiple assignment command as a
backwards-compatible extension to the Tcl _set_ command.

INTRODUCTION
--------------

Often one needs to assign values to several variables in close
proximity. Right now several _set_ commands are necessary:

set a 123
set b 456

or

set a 123; set b 456

Or one abuses the _foreach_ command:

foreach {a b} {123 456} break

However, by analogy to the _variable_ and _array set_ commands, the
following would be useful:

set a 123 b 456

This would assign 123 to the variable _a_ and 456 to the variable _b_.

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

SPECIFICATION
---------------

The _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 _set_ manual page; when an even number of arguments
is specified, the behaviour of _set v0 e0 ... vn en_ is identical to
that of the sequence of commands _set v0 e0; ...; set vn en_ according
to the traditional semantics.

The command _set v0 e0 ... vn en_ returns the value of _en_.

RATIONALE
-----------

This extension is an obvious analogy to the _variable_ and _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 (_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 _lassign_ (see [TIP #57]).
In particular, the _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.

REFERENCE IMPLEMENTATION
--------------------------

A patch to Tcl 8.4a3 which implements the _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 _set_ extensions have been devised
yet but this is easy to do once there is a consensus that this feature
is actually desirable.

COPYRIGHT
-----------

This document is placed in the public domain.

-------------------------------------------------------------------------

TIP AutoGenerator - written by Donal K. Fellows

[[Send Tcl/Tk announcements to tcl-an...@mitchell.org
Send administrivia to tcl-announ...@mitchell.org
Announcements archived at http://groups.yahoo.com/group/tcl_announce/
The primary Tcl/Tk archive is ftp://ftp.neosoft.com/pub/tcl/ ]]

Bruce Hartweg

unread,
Sep 4, 2001, 2:26:46 AM9/4/01
to
I wholeheartedly like this TIP. It is fully backwards compatible, it keeps
the same semantics, and makes code easier to read & write.

Bruce

"Anselm Lingnau" <ans...@strathspey.org> wrote in message news:pgpmoose.2001...@non.non.net...

Donal K. Fellows

unread,
Sep 4, 2001, 6:27:06 AM9/4/01
to Tcl Core Mailing List
Bruce Hartweg wrote:
> I wholeheartedly like this TIP. It is fully backwards compatible, it keeps
> the same semantics, and makes code easier to read & write.

The particular thing I like is being able to do a one-command swap:

set x $y y $x

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
-- I could even declare myself a religion, if that'd help.
-- Mark Loy <ml...@iupui.edu>

Bruce Hartweg

unread,
Sep 4, 2001, 6:52:55 AM9/4/01
to

"Donal K. Fellows" <fell...@cs.man.ac.uk> wrote in message news:3B94AC7A...@cs.man.ac.uk...

> Bruce Hartweg wrote:
> > I wholeheartedly like this TIP. It is fully backwards compatible, it keeps
> > the same semantics, and makes code easier to read & write.
>
> The particular thing I like is being able to do a one-command swap:
>
> set x $y y $x
>
Cool, another plus!

Bruce

0 new messages