code(n)
β¨ NAME β¨
itcl::code β bestie, this one captures the namespace context for a code fragment π like it's literally grabbing the vibe of wherever the code was born and keeping it safe for later.
πΈ SYNOPSIS πΈ
itcl::code ?-namespace name? command ?arg arg ...?
π¦ DESCRIPTION π¦
Okay so here's the tea β β code creates a scoped value for the command you hand it plus all its arg friends. And what even is a scoped value, you ask? It's a list, and it has exactly three elements, no more no less:
- the
"@scope"keyword - a namespace context
- a value string
Like, for example bestie, if you run this:
namespace foo {
code puts "Hello World!"
}
you get back this scoped value:
@scope ::foo {puts {Hello World!}}
See how it snapped a pic of where it was? πΈ The code command captures the current namespace context. But β plot twist β if you pass the -namespace flag, then the current context gets straight up ignored and the name string is used as the namespace context instead. Your call, your rules π
π why does this even matter tho
Extensions like Tk execute ordinary code fragments in the global namespace. Which is fine until it's not, yk? A scoped value grabs a code fragment together with its namespace context in a way that lets it be executed properly later on. It's giving "save the coordinates before you teleport" π§
It's needed, for example, to wrap up code fragments when a Tk widget is used inside a namespace:
namespace foo {
private proc report {mesg} {
puts "click: $mesg"
}
button .b1 -text "Push Me" -command [code report "Hello World!"]
pack .b1
}
The code fragment tied to button .b1 only makes sense in the context of namespace "foo". And on top of that, the "report" procedure is private, so it can only be accessed from within that namespace. The code command wraps the whole fragment up so it can be executed properly when the button gets pressed. Obsessed with the foresight honestly π
π it's a list replacement, no cap
Also note (important!) that code preserves the integrity of arguments on the command line. That makes it a natural glow-up replacement for the list command, which is often used to format Tcl code fragments.
So instead of using list like this:
after 1000 [list puts "Hello $name!"]
use code like this:
after 1000 [code puts "Hello $name!"]
This not only formats the command correctly, it also captures its namespace context. Two birds, one iconic move π¦π¦
π invoking scoped commands
Scoped commands can be invoked like ordinary code fragments β with or without the eval command. Both of these work properly, fr fr:
set cmd {@scope ::foo .b1}
$cmd configure -background red
set opts {-bg blue -fg white}
eval $cmd configure $opts
π«Ά the chaos clause (read this one)
Note that scoped commands by-pass the usual protection mechanisms. So this:
@scope ::foo {report {Hello World!}}
can be used to access the "foo::report" proc from any namespace context β even though it's private. Yes, even the private ones. The locks said no, the scope said watch me π Use this power responsibly bestie, that's main character energy with consequences.
β KEYWORDS β
scope, callback, namespace, public, protected, private
And that's the whole page! This was the code(n) manual page from itcl version 3.0 π thanks for reading all the way through, you ate that πβ¨
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- fedora 43: itcl 4.3.2-1.fc43 Β· TCL
- fedora 43: itcl 4.3.2-2.fc43 Β· TCL
- fedora 44: itcl 4.3.2-3.fc44 Β· TCL
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: incrtcl 4.2.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1993-1998 Lucent Technologies, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms