const(n)
π const(n) β Tcl Built-In Commands π
okay bestie, buckle up, because this page is about the const command from Tcl's built-in command set, and it's literally the "make it permanent" button for your variables π
β¨
π NAME π
const β create and initialize a constant π«Ά
πΈ SYNOPSIS πΈ
const varName value
π¦ DESCRIPTION π¦
So here's the vibe π: this command is normally used within a procedure body (or a method body, or a lambda term) to create a constant inside that procedure β or inside a namespace eval body to create a constant inside that namespace. Basically it makes a variable that nobody, and I mean nobody, is allowed to touch afterwards π€
The constant is an unmodifiable variable named varName, and it gets initialized with value. And the result of const? Always the empty string on success. Iconic, quiet, no drama π
If a variable varName does not exist yet, it gets created with its value set to value and gets marked as a constant π The way this slaps: that means no other command (like set, append, incr, unset β yeah, those ones π) may modify or remove the variable. And plot twist, variables are checked for whether they're constants before any traces are called π
If a variable varName already exists, that's an error β unless that variable is already marked as a constant, and in that case const is a total no-op, like "been there, done that" π
Now the rules, because there are always rules π varName may not be a qualified name and may not reference an element of an array by any means. And if the variable exists and it's an array? That's an error, bestie, no cap π
Constants are normally only removed by their containing procedure exiting or their namespace being deleted. That's it. That's the exit strategy π¦
β¨ 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}]
}
}
yes, that lappend line is indented like that in the original β copy it exactly, indentation and all, no notes π
π SEE ALSO π
proc(n), namespace(n), set(n), unset(n) β go read these next, bestie, they're the whole friend group π«Ά
π« KEYWORDS π«
namespace, procedure, variable, constant
and that's the page, periodt π this was const(n) from the Tcl Built-In Commands manual, version Tcl 9.0, dated... well, the footer just says Tcl 9.0 and honestly same, we don't do dates either π
β¨ love u, go make some unmodifiable variables π¦π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 2023 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms