Re: Why doesn't foreach return a value



On Feb 1, 1:17 am, Fredderic <my-name-h...@xxxxxxxxxx> wrote:

[return], [continue], or [break], all send a signal back indicating
special action needs to be taken. Each level in turn, then either
handles that signal or passes it back. This signal, which includes an
"all okay, go get the result" variant, is "returned".

Yes, more or less. Please forgive me for any disrespect I have shown
anyone in discussing this topic, I'll try one more time to use the
correct language.

Every Tcl command is implemented as a C level function. For now, I'm
not talking about Tcl procs, just the stuff implemented in C.

Every C level implementation of a Tcl command (except the one for
[exit], which doesn't return), returns a value, 'a standard Tcl
completion code', a small integer value. 0 = TCL_OK, 1 = TCL_ERROR, 2
= TCL_CONTINUE, 3 = TCL_BREAK 4 = TCL_RETURN. Greater than 4 are user
defined.

No C level implementation of a Tcl command returns any other value,
but there can be side effects. Some, but not all, of these command
implementations set, reset, append to or modify the Tcl interp's
result.

Is there any disagreement with this? Because if I got this wrong, my
further reasoning can't be right.

If this is correct, then, yes, every Tcl command is the same, and
[exit] isn't really a useful counter example.

But what use is there in talking about the internal return value of a
command at the script level? What we want at the script level is the
result of evaluation. This is what I thought I was talking about, and
hopefully the result will be meaningful.

The internal return value is not the interp result. The internal
return values are the control flow mechanism in Tcl. They are used to
indicate what to do next, internally. Any command which is executed
and has a return value other than TCL_OK causes control flow changes.
What changes occur is very specific to the context. But execution will
not resume at the next command of the enclosing context. Execution
doesn't resume, and the entire context is cleaned up. If this context
was a [foreach] body, then when a [break] is executed, it return 3.
This causes the entire context to be cleaned up. There is nothing to
return to for [break]. This return value of 3 gets returned to the C
implementation of the [foreach] command and this causes a C level
break. If instead a [continue] was executed in the same context, what
would happen is that the context would be destroyed and the return
value of 2 transmitted up to the same [foreach] implementation. But in
this case, the C implementation just starts the next iteration (if any
are left), which eventually creates another context for the body and
evaluates it. This is a different body context, one for each
iteration. Any exceptional return value in any evaluated script
causes the same behavior: abort the script evaluation, clean up and
return.

That obviously precludes commands which return an exceptional value
from 'returning' to the script from which they were invoked. But this
language is, as pointed out, misleading. Maybe it is more accurate to
say that Tcl evaluation does not return to the script if an
exceptional condition is returned by any command found in that script.
The script is destroyed, evaluation stops and control is returned to
the enclosing context.
.



Relevant Pages

  • Re: No argv, argc for worker thread
    ... I expected that a Tcl interpreter started in any manner would accept options ... This is how the interpreter works with command line options ... a "main" script processes command line ...
    (comp.lang.tcl)
  • Re: parsing script arguments in QuestaSim/ModelSim
    ... run a TCL script which invokes another script using the DO command. ... This will certainly tell you if you have a Tcl execution ...
    (comp.arch.fpga)
  • Re: A tcl command logger?
    ... >>log each command in the global namespace in a log file. ... tcl 8.4, I finally write a small script to do the task. ... 'expr' to make the evaluation. ...
    (comp.lang.tcl)
  • Re: how do you get a handle to the interpreter?
    ... > launch it from the tcl script via exec, rather than creating it as a tcl ... > made into a command ... Umm, if you are launching it via exec it is a complely different process, ...
    (comp.lang.tcl)
  • Re: Why doesnt foreach return a value
    ... applied to the context, not the loop boundary. ... are somewhat ambiguous at the Tcl level, ... Maybe you don't distinguish between what a command can cause ... Like, oops, not ...
    (comp.lang.tcl)