Version 0 of DebuggingExprCalls

Updated 2009-09-16 22:14:02 by seadevilgmailcom

An example problem:

set a 1 set b {} ;# <---- this represents bad data from elsewhere

expr {$a+$b} can't use empty string as operand of "+"

Which doesn't tell us which part of the expression was bad

set _debugExpr_ 1

if {[info exists _debugExpr_]} {
  rename expr _expr
  proc expr {args} {
    if {[catch {set result [uplevel _expr $args]}]} {
      error [uplevel list "error in expression: '" {*}$args "'"]
    }
    set result
  }
}

% expr {$a+$b}
error in expression: ' {$a+$b} '

Beware, this comes at the price of speed:
% time {expr $a/.5} 1000
25.975 microseconds per iteration
% time {_expr $a/.5} 1000
17.065 microseconds per iteration
% time {expr {$a/.5}} 1000
15.988 microseconds per iteration
% time {_expr {$a/.5}} 1000
0.709 microseconds per iteration