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

invalid bareword in expression

240 views
Skip to first unread message

heinrichmartin

unread,
May 4, 2018, 4:25:02 AM5/4/18
to
Hi,
out of curiosity: is there a reason for not accepting barewords as strings in expressions (expr/if).

background:
I know of [apply] and [list] to dynamically create commands, but in my particular use case I use the following pattern to inject a constant:

proc dynproc args [format {
# dynamically created proc body
...
if {%s eq ...} ... ;# does not always produce correct syntax
if {[::tcl::mathop::eq %s ...]} ... ;# 10% slower
...
} [list $const]]

Using (the string representation of a) [list] guaranties that the body has correct syntax in commands, but not in expressions. That's why I wonder whether there is a specific reason for this.

Regards
Martin

Arjen Markus

unread,
May 4, 2018, 4:53:31 AM5/4/18
to
I am not sure if there is a specific reason, but it is part of the general improvement of the [expr] command and related aspects of Tcl - see the various TIPs.

But what about: if {{%s} eq ...}

If you use the extra braces, the substituted string is acceptable and not a bare word. Unless you want to substitute %s by $variable ...

Regards,

Arjen

heinrichmartin

unread,
May 4, 2018, 5:14:11 AM5/4/18
to
On Friday, May 4, 2018 at 10:53:31 AM UTC+2, Arjen Markus wrote:
> But what about: if {{%s} eq ...}

I was thinking so, too, but it is not a general solution. This would work only if the canonical string representation of a list always uses backslash escapes.

A simple counterexample is [format {if {{%s} eq ...} ...} [list " "]]. Same for "%s"; and building the full expression with list is not a solution either.

briang

unread,
May 4, 2018, 3:49:56 PM5/4/18
to
The reason is that the [expr] language is not Tcl, it is it's own language. Different syntax, different semantics.

-Brian
0 new messages