Wednesday, 03 November 2010
expr shorthand syntax
One of the biggest complaints about Tcl is it’s verbosity for common expressions. Consider:
set sublist [lrange $list [expr {$a + 1}] [expr {$b - 1}]]
This is mitigated somewhat in list and string index and ranges
expression which support the notation int+int
or int-int
. However,
many simple, common expressions are still cumbersome.
There has been much discussion about shorthand notation for expr, however as for many things with Tcl, progress is slow or non-existent.
There is no need to suffer the same fate for Jim, though. So I
implemented a trivial patch to support the syntax $(...)
as a
shorthand for expr. Let’s see how it looks:
. set x $(3 + 4) 7 . incr y $(7 * [incr x]) 56 . set z $("bb" in {aa bb cc}) 1
A lot better! Essentially avoiding one set of brackets plus the
command name. Currently in Tcl, $(...)
is an accidental valid
expression, accessing an array named with the empty string. Giving
up this is a very small price to pay for this more accessible syntax.
Perhaps as Tcl grinds it’s way towards version 9.0, it could give
up a tiny bit of backward compatibility for easy of use.
Steve Bennett (steveb@workware.net.au)
comments powered by Disqus