TIP #133: Extending [expr] Operators
TIP #133: EXTENDING [EXPR] OPERATORS
======================================
Version: $Revision: 1.1 $
Author: Richard Suchenwirth <Richard.Suchenwirth-Bauersachs_at_siemens.com>
State: Draft
Type: Project
Tcl-Version: 8.5
Vote: Pending
Created: Tuesday, 08 April 2003
URL: http://www.*-*-*.com/
WebEdit: http://www.*-*-*.com/
Post-History:
-------------------------------------------------------------------------
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_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.
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}
COPYRIGHT
===========
This document has been placed in the public domain.
-------------------------------------------------------------------------
TIP AutoGenerator - written by Donal K. Fellows
Announcements archived at http://www.*-*-*.com/
Tcl/Tk at http://www.*-*-*.com/ ]]