Author: Eric Taylor
State: Draft
Type: Project
Created: 2023-06-06
Tcl-Version: 9.1
Tcl-Branch: tip-672
Abstract
This TIP extends the $ substitution to include expressions as $(expression).
Rationale and Discussion
The current method for expressions using the [expr]
command has 2
problems. First, it is difficult to BOTH read and write. Some, as in
TIP 282, go so far as to say it is ugly.
Secondly, as indicated in TIP 526, expressions should be braced, or there can be problems with both security and performance.
Consider the following computation (from TIP 282):
set x [expr {5*$y + 6*$z}]
set w [expr {$x**2 + $y**2}]
set v [expr {$w**2 + $y**2}]
The [expr {...}]
constructs make the code considerably harder to
read. This TIP would propose that there be a shorthand as such:
set x $(5*$y + 6*$z)
set w $($x**2 + $y**2)
set v $($w**2 + $y**2)
The implementation of this should also eliminate the requirement for
the braces from above, as braces would be assumed and code would be
compiled as though the parentheses were braces.
Since this is a $
substitution, it would continue to work inside
double quotes with the expected result.
There would also be an advantage in some text editors, which already have parenthesis balancing and syntax coloring features.
Another Tcl implementation, JimTcl uses this $(...)
substitution and it
seems to work without problems.
Version and Incompatibility
This TIP would apply to version 9. of Tcl. Since 9.* would be a major release, it should allow for some incompatibilities.
One incompatibility is that it would require that the use of the null string ""
or {}
as an array name would require a change from using $ substituion to using
the 2 argument form of the set command.
Thus, to assign the variable ""
to a value, would still work for both scalars
and array variables, as in:
set {} foobar ;# scalar variable
set (index) "array value" ;# array variable
But the use of $(index)
would have to change by using the set command:
set var [set (index)]
One other possible incompatibility would be with the use of the subst command.
The issue here is whether $(...)
would be a command or variable substitution
that subst has options to suppress.
Examples
% lassign {3.0 4.0} a b
% set c $( sqrt($a**2 + $b**2) ) ;# 3 4 5 triangle
5.0
% lassign {1000 3} total number
% puts "ave = $( double($total)/$number )" ;# demo usage inside quotes
ave = 333.333333333
.canvas addtag enclosed $($x - 20) $($x + 20) $($y - 20) $($y + 20)
Specification
TBD
Implementation
TBD
Copyright
This document has been placed in the public domain.