This may sound silly. Bear with me. While playing around with expr
, I came across the following scenario
proc badExpr { a b } {
return expr $a+$b
}
proc goodExpr { a b } {
return [ expr {$a+$b} ]
}
puts "Bad Expression Result : --->[ badExpr 1 3 ]<-----"
puts "Good Expression Result : [ goodExpr 1 3 ]"
Output:
Bad Expression Result : ---><-----
Good Expression Result : 4
As you can see, the proc badExpr
is returning an empty string. Just out of curiosity, I am interested to know why it is returning an empty string ?
Funnily enough, a badExpr
would be like this:
proc badExpr { a b } {
return [expr $a+$b]
}
What you've actually got is is weirdExpr
:
proc badExpr { a b } {
return expr $a+$b
}
What does this really do? It sets things in the result dictionary. Tcl results should really be considered to be a triple of three things: a result code (a small integer, in this case 0
for OK — 1
would be for a thrown error, and there are few other ones you can usually ignore) a result value (what you normally return) and a result dictionary. The result dictionary is used mainly with errors, to hold things like the stack trace (used to populate the errorInfo
global variable), the line number on which an error occurred, a computer-readable description of the error (used to populate the errorCode
global variable), etc., but it can hold any old thing. And you're putting something really odd into it (because that's how return
works); the system-defined keys all begin with a -
to make them look like ordinary options (and the dictionary itself is often called the option dictionary).
Let's show this using the two-variable form of catch
(the second variable catches the dictionary):
% proc weirdExpr { a b } {
return expr $a+$b
}
% catch {weirdExpr 1 3} p q
0
% puts $p
% puts $q
expr 1+3 -code 0 -level 0
Yes, there's a funny entry in there called expr
whose value is 1+3
…