const(3tcl)

πŸ“¦
version 9.0.4 Β· 2 versions
, in ubuntu resolute, ubuntu stonking

✨ 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 const is 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