TIP #133 Version 1.1: Extending [expr] Operators

This is not necessarily the current version of this TIP.


TIP:133
Title:Extending [expr] Operators
Version:$Revision: 1.1 $
Author:Richard Suchenwirth <Richard dot Suchenwirth-Bauersachs at siemens dot com>
State:Draft
Type:Project
Tcl-Version:8.5
Vote:Pending
Created:Tuesday, 08 April 2003

Abstract

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 "&&", "||", "!".

Rationale

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 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_oper