Re: append and read-traces



Andreas Leitgeb wrote:
I recently noticed, that in the following case, the behaviour is surprising (to my understanding):

% set x abc; proc handler args {puts >$args<}
% trace variable x r handler ;# (old syntax, ditto for new)

% lappend x def
x {} r<
abc def
% # read trace fired: ok.

% append x ghi
abc defghi
% # no read trace fired! Which is even more
% # strange, as even [info exists x] does
% # trigger it, and one might expect "append"
% # to at least check for existence of the var
% # first.

Did I miss something in the docs that explains this
difference, or is it a (long standing) bug?

Seen in all these: 8.5.2 8.4.12 8.3.3
Present probably since addition of traces.

Indeed: lappend fires read traces, append does not. Not sure since when, but at least since I started looking at the code!

This implies that an attempt to give a "default value" to a var fails when appending to it, but succeeds for lappending.

% proc handler {name1 name2 op} {
upvar 1 $name1 v
set w [expr {[info exists v]? $v : {}}]
set v "$w FOO!"
}
% trace add variable x read handler
% trace add variable y read handler
% lappend x foo
FOO! foo
% append y foo
foo

I strongly dislike this lack of symmetry, but it is a "mild" one ... and old. Is it worth risking breaking compat? Definitely in 9.*

.



Relevant Pages