trace causes generation of string representation of "commandPrefix" argument
commandPrefix argument, the
string representation of that list is generated:
proc p1 {list args} {
puts [list in . p1 [::tcl::unsupported::representation $list]]
}
set list [list one two three]
puts [list initial [::tcl::unsupported::representation $list]]
set var1 {}
trace add variable var1 read [list p1 $list]
set list [list p1 one two three]
set var1
Output:
initial {value is a list with a refcount of 3, object pointer at 0x55f4052f7ad8, internal representation 0x55f4052db288:(nil), no string representation}
in . p1 {value is a pure string with a refcount of 3, object pointer at 0x55f4051a8aa8, string representation "one two three"}
This going to concat by trace add (which currently does not differentiate between canonical and not-canonocal arguments):
Still worse is IMHO the duplication with resulting loss of real object type (it will be to pure string inside the call of trace).
unset -nocomplain var1
proc p1 {list args} {
puts "in p1:\t\t[::tcl::unsupported::representation $list]"
}
set list [list one two three]; set var1 {}
puts "init:\t\t[::tcl::unsupported::representation $list]"
trace add variable var1 read [list p1 $list]
puts "trace added:\t[::tcl::unsupported::representation $list]"
set var1
I found this several years ago and fixed already in my own branches.
I'm sure I had already a ticket for this, but can not find it anymore.
Anyway I'll push a branch next week (for >= 8.5) which closes it.
Likely related to https://core.tcl-lang.org/tcl/tktview?name=3485833 and the branch bug-3485833
Strictly speaking this is not a bug. String generation is a functionally valid thing Tcl or extensions may do. That said, it can be unwelcome, and any changes in the innards of [trace] that reduce the necessity of string generation seem like a good idea.