CONST in the Tcl 8.4 APIs
The const keyword in C is used to create a read-only variable. Once it is set, it cannot be modified. A common use of CONST is in parameter declarations to imply that the parameter cannot be modified by a procedure. The Tcl API definitions use a CONST macro instead of const to allow for older compilers that do not support const.
Several of the Tcl APIs were changed in Tcl 8.4 to include CONST where they previously did not. The most significant change is in the signature of string-based command procedures like RandomCmd in this chapter. Changes to tcl.h are generally backward compatible, so this change met with some debate. Most liked the addition of CONST because it allows better error checking, but the changes cause compiler warning messages if you compile old code with the 8.4 tcl.h. In some organizations, even compiler warnings are not allowed in code, so you may be compelled to clean up your code.
There are two reasons you may not be able to change older code. First, you may need to compile the same code against older and newer versions of Tcl. Second, you may not have the time to clean up the code. CONST definitions have a tendency to percolate throughout your code. To support these scenarios, 8.4 adds compile-time defines that change the effect of the CONST additions. Table 47-1 describes these definitions:
Table 47-1. Defines to control the meaning of CONST in the Tcl APIsNO_CONST | This defines CONST to nothing so no const keywords are used at all. This define has existed for some time. | USE_NON_CONST | Do not use any of the new CONST keywords added in Tcl 8.4. | USE_COMPAT_CONST | Only use the CONST keywords added for the return values of the Tcl 8.4 APIs. Almost all APIs return CONST values now. |
|