Tcl Source Code

View Ticket
Login
Ticket UUID: 1049119
Title: append and lappend with one arg undocumented & inconsistent
Type: Bug Created on: 2004-10-18 10:20:28
Submitter: nobody Assigned to: dgp
Subsystem: 16. Commands A-H Severity:
Priority: 5 Medium Last modified: 2026-11-13 11:18:46
Status: Open Closed by:
Resolution: None Closed on:
Version: None
Description:
Behavior of append and lappend with one argument is
undocumented and inconsistent.

Using both lappend and append with just one argument is
allowed
according to 8.5 (and likely all previous) manual
pages, arguments to
both are specified as:

  varName ?value value value ...?

It is however undocumented what happens when just
varName is
present.

Manual suggests consistent behavior of lappend and append:

  "Lappend is similar to append except that the values
are appended 
   as list elements..."

However in 8.3 (and likely in all versions), lappend
can create new
variable this way while append cannot:

  ~>tclsh
  % append x
  can't read "x": no such variable
  % lappend y
  % 

This should probably be considered as bug in append as
the manual says:

  "If varName doesn't exist, it is given a value equal
to the
   concatenation of all the value arguments."

so one might expect that concatenation of no value
arguments is an
empty string which should become the value of newly
created variable.

Furthermore, it is unclear whether lappend with one
argument will
normalize any pre-existing value into the canonical
list form or
not. In 8.3 it will not, which might be regarded as an
inconsistency
with behavior when used with more arguments:

  % set x { a}
  % puts /[lappend x]/
  / a/
  % puts /[lappend x b]/
  /a b/

This slight difference might become important if
applications use
current behavior of "lappend x" as a nice way to create
x or leave it
unchanges if it exists. Future versions of tcl might
change this
undocumented 'feature' and break these applications.

Both lappend and append are quite basic commands and
should not 
have uncertain behavior.

Recommended action:

a. fix append so that "append x" can create x
b. document behavior of "append x" and "lappend x"

Alternatively, consider these (IMHO inferior) solutions
instead:

c. document that "append x" cannot create x while
"append x a" can
d. change lappend so that "lappend x" list-normalizes x

(By a quick manual scan, I did not find any other list
operation which
could become no-op like "lappend x" with pre-existing
x. If there is
any, it should be made consistent whether they all
list-normalize x or
not.)

e. disallow "append x" and "lappend x"

This last solution would be the worst (IMHO) as is
would deprive
users of very convenient:

  lappend x
  set x [concat x y]

which creates x when needed and works regardless of (d.)

Vaclav Hanzl, email:  hanzl (x@x) noel.feld.cvut.cz
User Comments:
dkf added on 2004-10-19 22:45:15:
Logged In: YES 
user_id=79902

Hmm, is the difference between [append] and [lappend]
deliberate or accidental?  Your opinion, Miguel?

nobody added on 2004-10-20 15:27:21:
Logged In: NO 

I would bet that behavior of [append x] is accidental.

While I do not see reasons for a deliberate
difference, I see some difference in motivation
to mess with [append x] and [lappend x].

If [append x] fails to do what I expect it to do,
I can use [append x ""] instead. There would
be no such replacement for defunct [lappend x].
It is therefore easier for [append x] to escape
attention.

If somebody discovered that current strange
behavior of [append x] is exactly equivalent
to [set x], did not submit a bug report and went
on to create programs dependent on this, than
he well deserves some punishment like 
incompatible change of [append x] :-)

On the other hand, [lappend x] is currently
equivalent to [if {![info exists x]} {set x ""}]
(well, return value is different) and might
be used as a nice shorthand.
Some smarties might even routinely do:

eval lappend x $y

instead of:

if {![info exists x]} {
  set x {}
}
set x [concat $x $y]

After some googling for "eval lappend" and
"eval append" I see that both are used
and fixing [eval x] might actually fix more
programs than it breaks.

Vaclav Hanzl, email: hanzl (x@x) noel.feld.cvut.cz

msofer added on 2005-04-16 23:12:39:
Logged In: YES 
user_id=148712

Sorry, this somehow flew under my radar.

Looks accidental to me, although it is explicitly tested for
in the testsuite: append-3.3, append-4.6  

I tend to agree with Vaclav as to what "should" happend.
OTOH, this is not the only inconsistency between [append]
and [lappend]: the second will call read traces on the
variable, while the first will not (see FR 433252).

I hesitate to change something that is as fundamental and
has existed forever without a *lot* of very public
discussion. And probably not in a minor release anyway. Opinion?

nobody added on 2005-05-28 05:27:27:
Logged In: NO 

I absolutely agree that more discussion is needed. Trying to
re-align a cornerstone is worth thinking about as it can be
both good and bad idea. Maybe it should become two oposed
TIPs, the first being "Fix append x" and the second being
"Document current strange append x".
For the minor release, there is an ugly but honest option to
add some warning to the append man page ("Do not make any
assumptions about append x, it is strange and may be
reconsidered").
Would be nice to be able to grep World's tcl codebase for
"eval append" and count bugs caused by the fact that author
was unaware of the current behavior of append.
Googling reveals some related older discussion:
http://groups.yahoo.com/group/wintcl/message/740

Vaclav Hanzl

dkf added on 2005-12-16 18:14:06:

data_type - 360894

dkf added on 2009-07-13 01:27:16:
Passing to author of TIP#323 for consideration (see also issue:1227172).