TIP 292: Allow Unquoted Strings in Expressions

EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA
State:		Withdrawn
Type:		Project
Tcl-Version:	8.7
Vote:		Pending
Author:		Brian Griffin <[email protected]>
Created:	01-Nov-2006
Keywords:	Tcl, expr


This TIP proposes allowing unquoted words to be recognized as strings in expressions (expr, if, while).


Currently the following fails with a syntax error:

    set foo bar
    expr {$foo eq bar}

This seems antithetical to the EIAS Tao of Tcl. The set command does not require the quotes, so why should expr, especially since the operands of the eq and ne operators are treated as strings. It also seems reasonable for the == and != operators to reject unquoted strings as not a valid operand. This provides some enforcement for numerical vs. string comparison by forcing the use of quotes to convert the numeric == to a string equality test.

The rationale for making such a change is it will make complex code a bit easier to read. For example, I can:

        set var ENUM_VALUE_ONE

        switch $var {
                ENUM_VALUE_ZERO { ... }
                ENUM_VALUE_ONE  { ... }
                default { ... }

        if {[lsearch $supplied_values ENUM_VALUE_ONE] >= 0} { ... }

        if {[string equals $var ENUM_VALUE_ONE]} { ... }

but I must:

        if {$var eq "ENUM_VALUE_ONE"} { ... }

which could lead someone reading the code to incorrectly conclude that ENUM_VALUE_ONE is not the same thing as "ENUM_VALUE_ONE" or miss the fact that they are the same, especially when using a syntax highlighting editor.

Proposed Change

Change the expression parser to accept unquoted words that are not function names as strings. Modify EqualityExpr to reject unqoted strings for "==", but allow them for "eq".

Draft Implementation

None, at the moment.


This document has been placed in the public domain.