'''namespace code''' ''script'' Captures the current namespace context for later execution of the script ''script''. It returns a new script in which script has been wrapped in a '''[namespace inscope]''' command. The new script has two important properties. First, it can be evaluated in any namespace and will cause script to be evaluated in the current namespace (the one where the '''namespace code''' command was invoked). Second, additional arguments can be appended to the resulting script and they will be passed to ''script'' as additional arguments. For example, suppose the command '''set script [[namespace code {foo bar}]]''' is invoked in namespace '''::a::b'''. Then '''eval "$script x y"''' can be executed in any namespace (assuming the value of '''script''' has been passed in properly) and will have the same effect as the command '''::namespace eval ::a::b {foo bar x y}'''. This command is needed because extensions like Tk normally execute callback scripts in the global namespace. A scoped command captures a command together with its namespace context in a way that allows it to be executed properly later. ---- From the [Tcl chatroom] on 2004-11-03: dkf RS: You can probably hack something like autoexpansion with the help of [namespace code] suchenwi Ah. dkf: proc lambdaish args {uplevel 1 [list ::namespace code $args]} % [lambdaish format %.3f] [expr {atan2(0,-1)}] 3.142 suchenwi Cool, Donal! It's not exactly lambdaish, as it doesn't take a parameter list... which is half a lambda. Why not just call it "curry"? % [curry format %.3f] [expr {atan2(0,-1)}] 3.142 suchenwi Somehow makes more sense to me... dkf Pop that on the wiki. It also should work with any version of Tcl from 8.0 onwards. jenglish RS: the 'namespace code' trick, alas, is not cool at all -- it relies on an undocumented, hairy, and almost-certainly-incorrect misfeature of [unknown]. dkf It's an intended feature of [unknown]. It's a crazy aunt and not just a bug. ---- See also: * [namespace] * [namespace current] * [namespace eval] * [namespace inscope] * [eval] ---- [Category Command] - [Tcl syntax help]