Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TCL [return] inside self-written control constructs

84 views
Skip to first unread message

tombert

unread,
Jan 18, 2018, 9:05:14 AM1/18/18
to
Hi all,

I am failing on this simple example. Is it possible that the [return] inside the [instate dummy ...] call exits the [test] procedure?
I am seeing both a and b printed, but I need to see a only.

proc test {} {
puts a
instate dummy {
puts "inside instate call"
return
}
puts b
}

proc instate {statelist args} {
lassign $args trueCode else falseCode
return [uplevel 1 $trueCode]
}

thx

Don Porter

unread,
Jan 18, 2018, 9:57:30 AM1/18/18
to
For an example like this, the simplest effective answer is to use
[tailcall].

package require Tcl 8.6
proc test {} { # As above }
proc instate {statelist args} {
lassign $args trueCode else falseCode
tailcall eval $trueCode
}

% test
a
inside instate call

In the more general case, you'll want to examine how to [catch] and then
re-raise exceptions. See the [return] documentation:

http://www.tcl.tk/man/tcl8.6/TclCmd/return.htm

proc instate {statelist args} {
lassign $args trueCode else falseCode
catch {uplevel 1 $trueCode} m o
dict incr o -level
return -options $o $m
}

Polishing up details in the -errorinfo is left as an optional exercise
(see TIP 90).

--
| Don Porter Applied and Computational Mathematics Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

tombert

unread,
Jan 18, 2018, 5:06:54 PM1/18/18
to
thx - I have it now running:

set code [catch {uplevel 1 $trueCode} msg copt]
return -options $copt -code $code $msg

-----


is the [dict incr o -level] really necessary?

Don Porter

unread,
Jan 19, 2018, 8:35:01 AM1/19/18
to
I suspect there's some set of code and options dictionary for which that
approach will not produce the proper result, but I don't have the spare
cycles to hunt the examples down right now.

Andreas Leitgeb

unread,
Jan 28, 2018, 4:52:51 AM1/28/18
to
tombert <tomber...@live.at> wrote:
> set code [catch {uplevel 1 $trueCode} msg copt]
> return -options $copt -code $code $msg
> -----
> is the [dict incr o -level] really necessary?

My expectation is, that *without* the dict incr copt -level
then in your example

proc test {} {
puts a
instate dummy {
puts "inside instate call"
return
}
puts b
}

you'll still see the "b" written out.

0 new messages