TIP 677: Constant Variables

Login
Author:         Donal Fellows <[email protected]>
State:          Draft
Type:           Project
Vote:           Pending
Created:        29-Jun-2023
Tcl-Version:    9.0
Tcl-Branch:     tip-677

Abstract

This TIP proposes a way to define constants in Tcl code.

Rationale

While Tcl doesn't need that many constants by comparison with many other languages, as we can use symbolic names directly (witness how we handle flags to open), there are still the occasional thing where we want to have a value in a variable that isn't written to again.

An example of such is where you store a complex regular expression in a variable in order to give it a more mnemonic name. Rewriting such is going to be an infrequent action, as it will require reworking all the code that uses it as well; for the duration of the execution of a procedure or existence of a namespace, the variable is not to be changed.

It's possible to effectively make such variables by using a trace to set a variable back, but that's quite messy.

The advantage of having a special mechanism for this is that it is much easier to see the intention on the part of the author of the code that the value in a variable not be ever modified during the running of the code. This will also permit more efficient code generation at some point; a known constant is much more likely to be analysable during the generation of bytecode (or during any other possible future execution strategy).

Specification

This TIP proposes a new command, const that defines a "constant" variable in the current scope. It may be used either in a procedure (or method or lambda) to define a constant variable with local scope, or in a namespace (or at the top level) to define a constant variable with scope to that namespace (according to usual variable scoping rules).

The varName may be a qualified name. The varName must not be the name of an array element. Arrays may not contain constant variables by any means. The result of the const command will be the empty string.

A constant variable may be read like any other variable (but the bytecode compiler may replace its use in code with something more efficient).

A constant variable may not be written to or unset, except:

Write and unset traces will not fire on failed writes to or unsets of a constant variable, except (for unset traces only) when the containing scope is being deleted. The call to const on an existing constant will not fire a write trace even if the call succeeds.

The variable must not exist prior to const creating a constant variable (unless it is a constant variable with the same value). That is, $varName must be in a state where it will fail; it may be an upvar alias to a non-existing variable, providing that non-existing variable is not an element in an array.

Known Consequence

If a constant variable is created in the global namespace, it necessarily will endure with that value until the interpreter terminates.

Copyright

This document has been placed in the public domain.