Version 2 of Guarded proc

Updated 2005-09-09 16:31:24 by lwv

In more complex Tcl software, it may happen that a procedure is defined twice with different body and/or args, causing hard-to-track errors. The Tcl command 'proc' itself doesn't complain if it is called with an existing name. Here's one way to add this functionality. Early in your code, you overload the proc command like this:

  rename proc _proc
  _proc proc {name args body} {
    if {[info commands $name]!=""} {
        puts stderr "warning: [info script] redefines $name"
    }
    _proc $name $args $body   
  }

From the time that is sourced, any attempt to override a proc name will be reported to stderr (on Win-wish, it would show on the console in red). You may make it really strict by adding an "exit" after the "puts stderr ..."

Known feature: proc names with wildcards will run into this trap, e.g.

   proc * args {expr [join $args *]*1}

will always lead to a complaint because "*" fits any proc name. Fix (some regsub magic on 'name') left as an exercise.


Tcl/Tk is a wish come true ;-) Richard Suchenwirth 1999-07-19


Category Debugging