uplevel and {*}$args -- variable number of args.

49 views
Skip to first unread message

Robert Heller

unread,
Jun 6, 2022, 3:41:17 PM6/6/22
to
I am strugling with argument lists that contain lists and what happens with
uplevel and {*}$args. The lists are getting flattened. How can I preserve
the sub-lists? There has got to be way of doing this, but I am failing to
manage it.

uplevel #0 $options(-callback) PLANETARY_ORBIT [from arglist -epoch] \
"[from arglist -position]" "[from arglist -velocity]"

Then in the callback proc:

method _clientCallback {cmd epoch args} {
switch $cmd {
...
PLANETARY_ORBIT {
$self setposval {*}$args
}
...
}
}

method setposval {pos vel} {
..
}

The -position and -velocity options in the original arglist are lists of three
numbers, that eventually end up at pos and vel in setposval, but setposval is
getting 6 arguments instead. Somewhere between uplevel, the args argument to
_clientCallback and the use of {*}$args (and yes, this is all happening inside
of SNIT types). Oh, I am using Tcl 8.6.11

--
Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

clt.to...@dfgh.net

unread,
Jun 6, 2022, 5:55:37 PM6/6/22
to

>I am strugling with argument lists that contain lists and what happens with
>uplevel and {*}$args. The lists are getting flattened. How can I preserve
>the sub-lists?

>uplevel #0 $options(-callback) PLANETARY_ORBIT [from arglist -epoch] \
> "[from arglist -position]" "[from arglist -velocity]"

uplevel wants a list of words, try making the command script a list:

uplevel #0 [list $options(-callback) PLANETARY_ORBIT [from arglist -epoch] \
[from arglist -position] [from arglist -velocity]]


Assumes the result of [from] can be interpreted as a list.

Dave B

briang

unread,
Jun 6, 2022, 7:39:09 PM6/6/22
to
On Monday, June 6, 2022 at 12:41:17 PM UTC-7, Robert Heller wrote:
> I am strugling with argument lists that contain lists and what happens with
> uplevel and {*}$args. The lists are getting flattened.

Just remember that [uplevel] is another form of [eval]. With these commands, the argument list is processed twice, once by the execution of [eval] and again when [eval] executes the requested command. This means that arguments that require proper quoting need to be quoted twice. This is, in essence, what happens with Dave's posted solution.

-Brian

Mole Cool

unread,
Jun 7, 2022, 4:55:38 AM6/7/22
to
Just use eval, if you start quoting it may get ugly :-)

uplevel #0 $options(-callback) PLANETARY_ORBIT [from arglist -epoch] \
[list [from arglist -position] ] [list [from arglist -velocity]]

Then in the callback proc:

method _clientCallback {cmd epoch args} {
switch $cmd {
...
PLANETARY_ORBIT {
eval $self setposval $args

luocl

unread,
Jun 7, 2022, 5:50:05 AM6/7/22
to
On 6/7/22 4:55 PM, Mole Cool wrote:
> Just use eval, if you start quoting it may get ugly :-)
>
> uplevel #0 $options(-callback) PLANETARY_ORBIT [from arglist -epoch] \
> [list [from arglist -position] ] [list [from arglist -velocity]]
or this style

uplevel #0 [list $options(-callback) PLANETARY_ORBIT \
[from arglist -epoch] [from arglist -position] [from arglist -velocity]]

Reply all
Reply to author
Forward
0 new messages