TIP 133: Extending [expr] Operators

Author:         Richard Suchenwirth <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        08-Apr-2003
Tcl-Version:    9.1


This TIP proposes a way to define new operators for conditions and the expr command. It also includes demonstrations of how it might work in the examples: in tests inclusion in a list, and and, or, and not are aliases for "&&", "||", "!".


Inclusion of a value in a list is frequently tested with the construct

   if {[lsearch -exact $list $value] >= 0} {...}

The proposal, first brought by Reinhard Max in the Tcl Chatroom, is to allow an in operator in the language understood by expr, and the condition parts of for, if and while, so that the above can be written as

   if {$value in $list} {...}

This is shorter to type and much better to read.

In the same vein, I propose to allow operators "and", "or", "not" to be resolved exactly like the current "&&", "||" resp "!" The new "operator aliases" are not shorter than the original operators, but definitely better readable - and easier typed too, as no Shift (or Alt-Gr on German keyboards) key is needed.

When Tcl was young, almost all users knew C, so the C style operators were a good choice. In recent years, there is tendency that Tcl is used by persons who have no or less experience with C, but come from other languages (etc. BASIC variants have the AND, OR, NOT operators) or have Tcl as a first language. For all these, the option of natural-language operators will make the learning just a little bit easier.

Implementation Proposals

Donal K. Fellows remarked (on an earlier proposal relating to just an in operator) in the Tcl Chatroom: "On the plus side, it shouldn't be hard to implement (might need an extra opcode for lsearch, but that's pretty straightforward.)"

Pascal Scheffers proposed an extension mechanism for expr and conditions, so the proposed extensions to the expression language can be done in Tcl, with the commands:

 expr_register_operator in  {val list} {expr {[lsearch -exact $list $val]>=0}}
 expr_register_operator and {p q}      {expr {$p && $q}}
 expr_register_operator or  {p q}      {expr {$p || $q}}
 expr_register_operator not {p}        {expr {!$p}}

Such operator registrations can have one or two arguments (for unary and binary operators, respective) in the second argument. The third argument is a body that is evaluated, with local variables from the argument list substituted, and returns the resulting value, to be substituted for the operator and its operands.

Alternatively, one could stipulate that expr interprets its arguments in the above sense when called like this:

 expr operator in  {val list} {expr {[lsearch -exact $list $val]>=0}}
 expr operator and {p q}      {expr {$p && $q}}

This would currently raise an error, hence cannot break existing code.

For a simple start, it shall be an error to define an operator both as unary and binary.

Rules for operator precedence, and a way of specifying the precedence level, will still be needed.

A feature sometimes discussed in news:comp.lang.tcl, an assignment operator, could in the same way easily be added by users who so want:

 expr operator = {varName value} {upvar 1 $varName var; set var $value}

Reinhard Max has also proposed a unary empty operator:

  expr operator empty {list} {expr {[llength $list]==0}}


This document has been placed in the public domain.