Re: dict traces ...



Keith Nash wrote:
I think there is a problem of communication between those who are familiar
with the C implementation of Tcl, and those (like me) who view the
documentation as a set of rules and ask what is logically possible but
without much understanding of the Tcl internals.

set a 1
set b [dict create foo 2 bar 3]

Obviously one cannot trace the value of "1"; but it is possible to trace
reads and writes to a. Equally, one cannot trace the value of "2", but it
is possible to trace reads and writes to b, and if those reads and writes
occur by dictionary commands it is possible to trace whether they occur to
the key "foo". Though it is logically possible it may be at odds with the
way Tcl is implemented.

That is entirely possible. This is simply a regular trace on the variable that filters based on the dict key being accessed. However, you'd have to decide what to do in all the corner cases where the variable is accessed using something other than the dict commands, or changed to be a non-dict value. e.g.:

set foo {a 1 b 2}
dict trace foo a write ...
dict trace foo b unset ...
set foo {a b c}

What happens here? What traces fire (if any)? Is an error generated? Do the traces get removed?

I don't see any of those choices presenting themselves as obviously the correct approach. I also don't see why dicts need to support this functionality, given that we already have arrays and namespaces that support trace-able elements. You can even put a fully-qualified variable name into a dict and trace that.

-- Neil
.



Relevant Pages

  • Re: dict traces ...
    ... documentation as a set of rules and ask what is logically possible but ... without much understanding of the Tcl internals. ... Obviously one cannot trace the value of "1"; but it is possible to trace ... the key "foo". ...
    (comp.lang.tcl)
  • Re: syntax for dict
    ... trace on the variable that contains a list/dict. ... In Tcl, a list or dict is a value and never changes, so having a modification trace on a value doesn't make sense. ...
    (comp.lang.tcl)
  • Re: Tcl the misunderstood
    ... an article I wrote in order to promote Tcl, ... Anyway, I definitely lacked "trace" examples, maybe even combined with Tk ... entry widgets. ... understanding the power of the 'after' command. ...
    (comp.lang.tcl)
  • Re: Tracing a tcl script
    ... > a trace? ... Tcl procedure. ... command is invoked that contains the name of the command and its arguments. ... each bytecode instruction. ...
    (comp.lang.tcl)
  • Re: Limited initialization for non-limited types
    ... Trace_Stash: Trace; ... package body Foo is ... function Construct_Xreturn Nil; ... Why do I have a constructor function for X that returns Nil? ...
    (comp.lang.ada)