Version 14 of constants

Updated 2004-01-20 18:05:29

How do you create constants in Tcl programs? There are, of course, several ways.


LV: Well, one way to create a constant in a Tcl program is to just code it. For instance, in the line of code:

        set a "abc"

The string "abc" is a constant.

Note, in fact, that the string need not be in quotes:

        set b abc

also involves a constant string "abc".

But what about numeric constants?

Well, a decimal value can be set thusly:

        set c 10

or

        set d "10"

and, when $c or $d are used in Tcl commands which expect numeric values, the value of the variable will be treated as the number 10.

Note that Tcl versions up through 8.4.1 treat a leading 0 as indicating that the numeric value is octal:

        set e "010"

does not result in $e being equal to $d; instead, it is treated as being two less than $d.

Are there ways to express numeric constants in other bases, such as binary or hexidecimal?

 $ tclsh
 % set a 0xbad
 0xbad
 % incr a
 2990
 % 

So it appears that, with appropriate notation, one can get hexidecimal.

For binary, one needs to do this:

        proc fromBinary { digitString } {
            set r 0
            foreach d [split $digitString {}] {
                incr r $r
                incr r $d
            }
            return $r
        }
        puts [fromBinary 0101001010101010]
        puts [format %x [fromBinary 0101001010101010]]

or

        proc bits2int {bits} {
             #returns integer equivalent of a bitlist
             set bits [format %032s [join $bits {}]]
             binary scan [binary format B* $bits] I1 x
             set x
         }

procedure constants

Create a procedure for each constant, which returns the value.

  proc FLAG1 {} { return 0x0001 }

You then access the constant by calling the procedure, like [FLAG1]. This is easy to code, but gets clumsy when you have lots of constants. Another possibility is to use an array in one constant function, but this is fairly inflexible.

  proc CONST { key } {
    array set constant {
        FLAG1  0x001
        FLAG2  0x002
        PI  3.14159
    }
    return $constant($key)
  }

readonly trace

Brent Welch suggests using write variable traces to implement readonly variables. But since the write trace fires after the variable value has changed, you need to keep a cache of the original value somewhere.

George Howlett made a suggestion (at a Tcl conference tutorial) that read traces are your friend. He suggested a couple of very flexible procedures. (According to Don Porter's c.l.t. post)

  % proc _constant_read_trace {val name1 name2 ops} {
      upvar $name1 var
      set var $val
  }
  % proc constant {varName value} {
      uplevel [list trace variable $varName r [list _constant_read_trace $value]]
  }
  % constant PI 3.14159
  % set PI
  3.14159
  % set PI 3; # Only in Indiana :)
  3
  % set PI
  3.14159

RS: Slightly modified the above, so attempts to vary a constant raise an error (it probably was one ;-):

  proc _constant_trace {val name1 name2 ops} {
      upvar $name1 var
     if {$ops=="w"} {
        return -code error "constant $val may not be changed"
     }
      set var $val
  }
  proc constant {varName value} {
      uplevel [list trace variable $varName rw [list _constant_trace $value]]
  }
 % set PI 1.23
 can't set "PI": constant 3.14159 may not be changed

RS again: This raises no error, but keeps a constant with minimal code:

 proc const {name value} {
   uplevel 1 [list set $name $value]
   uplevel 1 [list trace var $name w "set $name [list $value];#" ]
 }

Simple error-raising variation:

 proc const {name value} {
   uplevel 1 [list set $name $value]
   uplevel 1 [list trace var $name w {error constant ;#} ]
 }
 % const x 11
 % incr x
 can't set "x": constant

Karl Lehenbauer, I think, gets credit for one invention of read-only variables [cite references].


See also Tcl and octal numbers. and Binary representation of numbers.