Re: Exception style



On Sat, 29 Apr 2006 20:51:48 GMT,
Darren New <dnew@xxxxxxxxxx> wrote:

I am for having a -nocomplain (or any other name) switch :)
I think that makes much more sense for commands that take variable
names rather than values. It's not uncommon that I accidentally [incr
$x] when I meant [incr x]. Right now, that usually gets caught.

I actually have a favourite little proc of mine to handle times when I
*do* want automatic initialisation...

proc incr2 {vari {incr 1} {init *} } {
upvar 1 $vari var
if { ! [catch [list incr var $incr] res] } {return $res}
if { [string is digit -strict $init] } {return [set var $init]}
set var $incr
}

There are probably better ways to do it, but it neatly handles every
case I've ever needed it to, including cases where the increment
value isn't obvious. That proc has a default "initialiser" value of
zero (pre-incr), but if you want it to be something different, you just
set it to exactly what you want it to be.

But I still use the standard [incr] wherever possible for exactly the
reason Darren mentioned (I've thrown a value at [incr] rather than the
variable name far too many times, and having TCL spit it back at me
is rather handy in a large script).


Fredderic
.



Relevant Pages

  • Re: Problems with serial ports
    ... use a form of busy wait that tcl pure-ists would never approve. ... proc wait { ... set str [binary format c1 $c] ...
    (comp.lang.tcl)
  • Re: So slow Tcl :(
    ... proc fib { ... Using incr is faster instead of expr... ... > procs to reduce proc call overhead. ... > set base ...
    (comp.lang.tcl)
  • Re: Counter
    ... proc generateID { ... counter::incr command upon entry. ...
    (comp.lang.tcl)
  • Re: question about a proc
    ... I am learning TCL right now, i created a simple proc following the ... which it views as the set command with 4 arguments - which is your error. ...
    (comp.lang.tcl)
  • Re: Upvar confusion
    ... specify the "level" argument and that argument should only ever be given ... proc should itself take a level argument (and incr it). ... This was back in Tcl 7.5 though, ...
    (comp.lang.tcl)