const(n)

πŸ“¦
version 9.0.3 Β· 2 versions
, in alpine 3.23, alpine 3.24, nixos 26.05, nixpkgs unstable

πŸ’– 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