Forum OpenACS Development: Re: Language lawyering

Collapse
8: Re: Language lawyering (response to 7)
Posted by Alfred Werner on
Don, it gets worse :)

Following your example from the docs -

$tclsh
% info tclversion
8.4
% set a 3
3
% set b {$a + 2}
$a + 2
% expr $b * 4
11
% if {$b * 4 == 11} {puts "yep"}
can't use non-numeric string as operand of "*"
% if {11 == $b * 4} {puts "yep"}
can't use non-numeric string as operand of "*"
% if {[expr $b * 4] == 11} {puts "yep"}
yep

Yikes! Damned if you do - damned if you don't. Or at least if you do - you have to do more than what might seem obvious at first reading.

It is worth noting that if you try:
set b $a + 2  it is an error - set takes only two operands/parameters
set b [expr $a + 2] is the correct form. So if you were INTENDING to defer the math and just pass the formula in some recursive lisp-ish type of way then you'd need to remember to later [expr] it.

I guess this is just a cautionary note for anyone introducing braces in their code without first really understanding the deep voodoo at work. OTOH, I think it would be safe to change a lot of == to eq within if clauses, and should the code break, there was probably something wrong-ish to begin with.