Why doesn't this work like I would think?
set x [if {1} {return 2} {return 3}]
The command output shows 2
, but when you check the value of x
, it is found to be undefined..!
I have read a workaround, ...
proc give {x} {return $x}
set x [if {1} {give 2} {give 3}]
...but I don't understand why the first one doesn't work.
From the tcl 8.5 documentation for if
:
"The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN."
Questions:
set x [if {1} {return 2} {return 3}]
set a value for x
?set x [if {1} {return 2} {return 3}]
doesn't work, how does tclsh
display "2
" on the interactive shell?tclsh
displays "2
" on the interactive shell, how is it that set x
remains undefined?See http://www.tcl.tk/man/tcl8.6/TclCmd/return.htm
Return codes are used in Tcl to control program flow. A Tcl script is a sequence of Tcl commands. So long as each command evaluation returns a return code of TCL_OK, evaluation will continue to the next command in the script. Any exceptional return code (non-TCL_OK) returned by a command evaluation causes the flow on to the next command to be interrupted. Script evaluation ceases, and the exceptional return code from the command becomes the return code of the full script evaluation. This is the mechanism by which errors during script evaluation cause an interruption and unwinding of the call stack. It is also the mechanism by which commands like break, continue, and return cause script evaluation to terminate without evaluating all commands in sequence.
So the evaluation of the script is aborted, x
does not get set, and 2
is returned and printed as a result.
The expr
command returns a value, so
set x [if 1 {expr 2} {expr 3}]
But if you're doing that, you might as well use the ternary operator
set x [expr {1 ? 2 : 3}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With