const(3tcl)
β¨ const β create & initialize a constant, bestie β¨
okay so this is a page from the Tcl Built-In Commands manual and honestly? it's giving "immutable variable" main character energy π
πΈ SYNOPSIS πΈ
const varName value
π DESCRIPTION π
okay so picture this: you want a variable that just cannot be messed with. that's const, and she ate fr fr π«Ά
you normally call this inside a procedure body (or a method body, or a lambda term) to make a constant that lives in that procedure. or you call it inside a namespace eval body to make a constant that lives in that namespace. the constant is an unmodifiable variable, her name is varName, and she gets initialized with value. and when it works? the result of const is literally always the empty string. iconic, lowkey humble of her β¨
so here's the vibe check π
- if a variable varName doesn't exist yet, she gets created, her value gets set to value, and she gets marked as a constant. that means no other command may modify or remove her β and yes bestie, we mean
set,append,incr,unset, all of them. and here's the tea: variables are checked for whether they are 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, in which case
constis just a no-op and everyone goes about their day π
now the rules, no cap:
- varName may not be a qualified name, and may not reference an element of an array by any means. periodt.
- if the variable exists and is an array? that's an error, sorry bestie π
and when do constants get removed? normally only when their containing procedure exits, or when their namespace gets deleted. that's it. she's not leaving any other way π
π EXAMPLES π
creating a constant in a procedure β she's local, she's protected, she's thriving:
proc foo {a b} {
const BAR 12345
return [expr {$a + $b + $BAR}]
}
creating a constant in a namespace β so you can factor out a regular expression like the organized queen you are π:
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 inside a loop does NOT error β she just gets recreated each time, no drama, no tears π§:
proc foo {n} {
set result {}
for {set i 0} {$i < $n} {incr i} {
const X 123
lappend result [expr {$X + $i**2}]
}
}
ngl the second time through that loop, the variable already exists and is already a constant β so const is a no-op. that's why it doesn't blow up π
π¦ SEE ALSO π¦
β KEYWORDS β
namespace, procedure, variable, constant
and that's the whole page bestie!! π this one's from Tcl 9.0, and the page itself is const(3tcl) β go forth and make your variables un-touchable, you absolute legend π«Άπβ¨
π¦ where this page comes from
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2023 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms