logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

const - create and initialize a constant

Description

       This  command  is  normally  used  within  a  procedure body (or method body, or lambda term) to create a
       constant within that procedure, or within a  namespaceeval  body  to  create  a  constant  within  that
       namespace.   The  constant  is  an unmodifiable variable, called varName, that is initialized with value.
       The result of const is always the empty string on success.

       If a variable varName does not exist, it is created with its value set to value and marked as a constant;
       this means that no other command (e.g., set, append, incr, unset) may  modify  or  remove  the  variable;
       variables are checked for whether they are constants before any traces are called.  If a variable varName
       already exists, it is an error unless that variable is marked as a constant (in which case const is a no-
       op).

       The varName may not be a qualified name or reference an element of an array by any means. If the variable
       exists and is an array, that is an error.

       Constants  are  normally  only  removed  by  their  containing procedure exiting or their namespace being
       deleted.

Examples

       Create a constant in a procedure:

              proc foo {a b} {
                  const BAR 12345
                  return [expr {$a + $b + $BAR}]
              }

       Create a constant in a namespace to factor out a regular expression:

              namespace eval someNS {
                  const FOO_MATCHER {(?i)}\mfoo\M}

                  proc findFoos str {
                      variable FOO_MATCHER
                      regexp -all $FOO_MATCHER $str
                  }

                  proc findFooIndices str {
                      variable FOO_MATCHER
                      regexp -all -indices $FOO_MATCHER $str
                  }
              }

       Making a constant in a loop doesn't error:

              proc foo {n} {
                  set result {}
                  for {set i 0} {$i < $n} {incr i} {
                      const X 123
                lappend result [expr {$X + $i**2}]
                  }
              }

Keywords

       namespace, procedure, variable, constant

Tcl                                                    9.0                                           const(3tcl)

Name

       const - create and initialize a constant

See Also

proc(3tcl), namespace(3tcl), set(3tcl), unset(3tcl)

Synopsis

constvarNamevalue
________________________________________________________________________________________________________________

See Also