TIP #292: Allow Unquoted Strings in Expressions


TIP:292
Title:Allow Unquoted Strings in Expressions
Version:$Revision: 1.3 $
Author:Brian Griffin <bgriffin at model dot com>
State:Draft
Type:Project
Tcl-Version:8.7
Vote:Pending
Created:Wednesday, 01 November 2006
Keywords:Tcl, expr

Abstract

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

Rationale

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.

Copyright

This document has been placed in the public domain.


Powered by Tcl[Index] [History] [Edit] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows