code(3itcl)
π NAME π
itcl::code β bestie, this is the lil glow-up that grabs the namespace context for a code fragment so it doesn't get lost out here π
(this is page code(3itcl) from the incr Tcl crew, btw π)
β¨ SYNOPSIS β¨
itcl::code ?-namespace name? command ?arg arg ...?
π¦ DESCRIPTION π¦
Okay so here's the tea β: code builds a scoped value for the command you hand it, plus all the arg arguments tagging along with it. And what even is a scoped value? It's a list with three elements, no cap:
- the literal keyword
"@scope" - a namespace context
- a value string
Like, watch this. If you run:
namespace foo {
code puts "Hello World!"
}
the scoped value that pops out is literally:
@scope ::foo {puts {Hello World!}}
Not me noticing that code captures the current namespace context π And if you pass the -namespace flag? Then the current context gets completely ignored, and the name string becomes the namespace context instead. Periodt.
πΈ why does this even matter, bestie
Extensions like Tk just go ahead and execute ordinary code fragments in the global namespace π which is a whole problem. A scoped value fixes this by capturing a code fragment together with its namespace context, in a way that lets it be executed properly later on. It's giving personal assistant for your code.
You need it, 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 way that the code fragment attached to button .b1 only makes sense in the context of namespace "foo" π Also? The "report" procedure is private, which means it can only be accessed inside that namespace. So the code command wraps the whole fragment up so it can be executed properly the moment that button gets pressed. Iconic behaviour, honestly π
β also it preserves your arguments, slay
Highkey one of the best parts: code preserves the integrity of arguments on the command line. That makes it the natural replacement for the list command β which, fun fact, is what people usually reach for when formatting Tcl code fragments.
So instead of doing this with list:
after 1000 [list puts "Hello $name!"]
you do this with code:
after 1000 [code puts "Hello $name!"]
Which not only formats the command correctly, but ALSO captures its namespace context π two birds, one code fragment, we love to see it.
π§ invoking scoped commands
Scoped commands can be invoked just like ordinary code fragments β with or without the eval command, your choice, main character energy either way. For example, all of these work properly:
set cmd {@scope ::foo .b1}
$cmd configure -background red
set opts {-bg blue -fg white}
eval $cmd configure $opts
π OKAY BUT REAL TALK WARNING β scoped commands by-pass the usual protection mechanisms. Which means 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 fr fr. So like, use that power responsibly bestie π
π KEYWORDS π
scope, callback, namespace, public, protected, private
okay that's the whole page, you ate it, go forth and namespace responsibly π
π«Ά this one's from itcl, version 3.0 β mwah, catch you on the next manpage bestie πβ¨
π¦ where this page comes from
- debian bookworm: tcl-itcl4-doc 4.2.3-1 Β· BSD-Tcl
- debian trixie: tcl-itcl4-doc 4.3.2-3 Β· BSD-Tcl
- ubuntu jammy: tcl-itcl4-doc 4.2.2-1 Β· BSD-Tcl
- ubuntu noble: tcl-itcl4-doc 4.2.4-1 Β· BSD-Tcl
- ubuntu questing: tcl-itcl4-doc 4.3.2-3
- ubuntu resolute: tcl-itcl4-doc 4.3.5-1 Β· BSD-Tcl
- ubuntu stonking: tcl-itcl4-doc 4.3.8-1 Β· BSD-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