TIP 292: Allow Unquoted Strings in Expressions

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
State:		Draft
Type:		Project
Tcl-Version:	8.7
Vote:		Pending
Post-History:	
Author:		Brian Griffin <bgriffin@model.com>
Created:	01-Nov-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.

History