Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 667ffd5e7dd32f7a7a422121a15563173a81efd1e63e5317f7c295c50c10d2f3
Ticket: 2969488fffffffffffffffffffffffffffffffff
trace variable doesn't fire if you upvar to an array element
User & Date: sbron on 2022-08-17 11:06:39
Changes

  1. closer changed to: "nobody"
  2. comment changed to:
    (This is FR 572889 from June 2002 converted to a bug)
    
    Tcl Versions: all 8.4 and onwards; confirmed on 8.4.11 and 8.6b1.1
    OS platform and version: probably all, confirmed on Windows XP + SP3
    
    Problem behaviour:
    If you set a trace on an array, then link to a specific element of that array using [upvar], any changes made via the linked variable will not cause traces to fire.
    
    Expected behaviour:
    If a trace has been placed on a variable, any access to that variable (even indirectly) should invoke the trace.
    
    Additional detail / Use case
    I have a "fileentry" megawidget that combines an entry, a button with an icon, and a tk_getOpenFile / tk_getSaveFile.  If the button is clicked the appropriate file dialog is displayed, and the selected filename written back into the -textvariable.  The obvious implementation is to link to the -textvariable using [upvar].  If the -textvariable is an array element then traces on the element will not fire.  In this case I am using variable traces to maintain consistency between various UI fields, so I lose consistency if the trace doesn't fire.
    
    Workarounds
    (1) Change the UI to use a scalar rather than an array element for this field
    (2) set and dereference the varname given by -textvariable directly rather than using [upvar]
    (3) have the megawidget use the entry's get/insert/delete methods rather than accessing -textvariable.
    
    Code sample
    ---
    proc tracer {args} { puts "<trace> $args" }
    set ui(field1) "alpha"
    trace add variable ui write "tracer write"
    trace add variable ui array "tracer array"
    
    set ui(field1) "beta" ;# trace write fires
    array set ui {field1 "gamma"} ;# trace write & array both fire
    
    proc alter {varname} {
      upvar #0 $varname v
      set v "delta"
    }
    
    alter ui(field1) ;# trace doesn't fire
    ---
    
  3. icomment:
    I just ran into this problem again today. Considering the multiple bug reports of similar issues over the last 20 years, I'm clearly not the only one.
    
    Tcl 9.0 seems like the perfect opportunity to rectify the situation. Can the fix attached to this ticket please make it into 9.0?
    
  4. login: "sbron"
  5. mimetype: "text/x-fossil-wiki"
  6. severity changed to: "Minor"