Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't tcl's "if" return values?

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:

  1. Why doesn't set x [if {1} {return 2} {return 3}] set a value for x?
  2. Given that set x [if {1} {return 2} {return 3}] doesn't work, how does tclsh display "2" on the interactive shell?
  3. Given that tclsh displays "2" on the interactive shell, how is it that set x remains undefined?
like image 955
LionKimbro Avatar asked Dec 05 '22 10:12

LionKimbro


2 Answers

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.

like image 77
flup Avatar answered Dec 19 '22 18:12

flup


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}]
like image 39
glenn jackman Avatar answered Dec 19 '22 19:12

glenn jackman